MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
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
45using namespace std;
46using namespace mfem;
47
48int 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();
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);
231 m->Assemble();
232 // shift the eigenvalue corresponding to eliminated dofs to a large value
233 m->EliminateEssentialBCDiag(ess_bdr, numeric_limits<real_t>::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(MPI_COMM_WORLD, argc, argv);
285 strumpack->SetPrintFactorStatistics(true);
286 strumpack->SetPrintSolveStatistics(false);
287 strumpack->SetKrylovSolver(strumpack::KrylovSolver::DIRECT);
288 strumpack->SetReorderingStrategy(strumpack::ReorderingStrategy::METIS);
289 strumpack->SetMatching(strumpack::MatchingJob::NONE);
290 strumpack->SetCompression(strumpack::CompressionType::NONE);
291 strumpack->SetOperator(*Arow);
292 strumpack->SetFromCommandLine();
293 precond = strumpack;
294 }
295#endif
296 }
297
298
299 HypreLOBPCG * lobpcg = new HypreLOBPCG(MPI_COMM_WORLD);
300 lobpcg->SetNumModes(nev);
301 lobpcg->SetRandomSeed(seed);
302 lobpcg->SetPreconditioner(*precond);
303 lobpcg->SetMaxIter(200);
304 lobpcg->SetTol(1e-8);
305 lobpcg->SetPrecondUsageMode(1);
306 lobpcg->SetPrintLevel(1);
307 lobpcg->SetMassMatrix(*M);
308 lobpcg->SetOperator(*A);
309
310 // 9. Compute the eigenmodes and extract the array of eigenvalues. Define a
311 // parallel grid function to represent each of the eigenmodes returned by
312 // the solver.
313 Array<real_t> eigenvalues;
314 lobpcg->Solve();
315 lobpcg->GetEigenvalues(eigenvalues);
316 ParGridFunction x(fespace);
317
318 // 10. Save the refined mesh and the modes in parallel. This output can be
319 // viewed later using GLVis: "glvis -np <np> -m mesh -g mode".
320 {
321 ostringstream mesh_name, mode_name;
322 mesh_name << "mesh." << setfill('0') << setw(6) << myid;
323
324 ofstream mesh_ofs(mesh_name.str().c_str());
325 mesh_ofs.precision(8);
326 pmesh->Print(mesh_ofs);
327
328 for (int i=0; i<nev; i++)
329 {
330 // convert eigenvector from HypreParVector to ParGridFunction
331 x = lobpcg->GetEigenvector(i);
332
333 mode_name << "mode_" << setfill('0') << setw(2) << i << "."
334 << setfill('0') << setw(6) << myid;
335
336 ofstream mode_ofs(mode_name.str().c_str());
337 mode_ofs.precision(8);
338 x.Save(mode_ofs);
339 mode_name.str("");
340 }
341 }
342
343 // 11. Send the solution by socket to a GLVis server.
344 if (visualization)
345 {
346 char vishost[] = "localhost";
347 int visport = 19916;
348 socketstream mode_sock(vishost, visport);
349 mode_sock.precision(8);
350
351 for (int i=0; i<nev; i++)
352 {
353 if ( myid == 0 )
354 {
355 cout << "Eigenmode " << i+1 << '/' << nev
356 << ", Lambda = " << eigenvalues[i] << endl;
357 }
358
359 // convert eigenvector from HypreParVector to ParGridFunction
360 x = lobpcg->GetEigenvector(i);
361
362 mode_sock << "parallel " << num_procs << " " << myid << "\n"
363 << "solution\n" << *pmesh << x << flush
364 << "window_title 'Eigenmode " << i+1 << '/' << nev
365 << ", Lambda = " << eigenvalues[i] << "'" << endl;
366
367 char c;
368 if (myid == 0)
369 {
370 cout << "press (q)uit or (c)ontinue --> " << flush;
371 cin >> c;
372 }
373 MPI_Bcast(&c, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
374
375 if (c != 'c')
376 {
377 break;
378 }
379 }
380 mode_sock.close();
381 }
382
383 // 12. Free the used memory.
384 delete lobpcg;
385 delete precond;
386 delete M;
387 delete A;
388#if defined(MFEM_USE_SUPERLU) || defined(MFEM_USE_STRUMPACK)
389 delete Arow;
390#endif
391
392 delete fespace;
393 if (own_fec)
394 {
395 delete fec;
396 }
397 delete pmesh;
398
399 return 0;
400}
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.
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
virtual const char * Name() const
Definition fe_coll.hpp:79
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
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
NURBSExtension * NURBSext
Optional NURBS mesh extension.
Definition mesh.hpp:290
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).
int GetNKV() const
Definition nurbs.hpp:491
Arbitrary order non-uniform rational B-splines (NURBS) finite elements.
Definition fe_coll.hpp:682
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
void mfem_error(const char *msg)
Definition error.cpp:154
const char vishost[]