MFEM  v4.2.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
ex1p.cpp
Go to the documentation of this file.
1 // MFEM Example 1 - Parallel Version
2 // SuperLU Modification
3 //
4 // Compile with: make ex1p
5 //
6 // Sample runs: mpirun -np 4 ex1p -m ../../data/square-disc.mesh
7 // mpirun -np 4 ex1p -m ../../data/star.mesh
8 // mpirun -np 4 ex1p -m ../../data/star-mixed.mesh
9 // mpirun -np 4 ex1p -m ../../data/escher.mesh
10 // mpirun -np 4 ex1p -m ../../data/fichera.mesh
11 // mpirun -np 4 ex1p -m ../../data/fichera-mixed.mesh
12 // mpirun -np 4 ex1p -m ../../data/toroid-wedge.mesh
13 // mpirun -np 4 ex1p -m ../../data/periodic-annulus-sector.msh
14 // mpirun -np 4 ex1p -m ../../data/periodic-torus-sector.msh
15 // mpirun -np 4 ex1p -m ../../data/square-disc-p2.vtk -o 2
16 // mpirun -np 4 ex1p -m ../../data/square-disc-nurbs.mesh -o -1
17 // mpirun -np 4 ex1p -m ../../data/star-mixed-p2.mesh -o 2
18 // mpirun -np 4 ex1p -m ../../data/disc-nurbs.mesh -o -1
19 // mpirun -np 4 ex1p -m ../../data/pipe-nurbs.mesh -o -1
20 // mpirun -np 4 ex1p -m ../../data/ball-nurbs.mesh -o 2
21 // mpirun -np 4 ex1p -m ../../data/star-surf.mesh
22 // mpirun -np 4 ex1p -m ../../data/square-disc-surf.mesh
23 // mpirun -np 4 ex1p -m ../../data/inline-segment.mesh
24 // mpirun -np 4 ex1p -m ../../data/amr-quad.mesh
25 // mpirun -np 4 ex1p -m ../../data/amr-hex.mesh
26 // mpirun -np 4 ex1p -m ../../data/mobius-strip.mesh
27 //
28 // Description: This example code demonstrates the use of MFEM to define a
29 // simple finite element discretization of the Laplace problem
30 // -Delta u = 1 with homogeneous Dirichlet boundary conditions.
31 // Specifically, we discretize using a FE space of the specified
32 // order, or if order < 1 using an isoparametric/isogeometric
33 // space (i.e. quadratic for quadratic curvilinear mesh, NURBS for
34 // NURBS mesh, etc.)
35 //
36 // The example highlights the use of mesh refinement, finite
37 // element grid functions, as well as linear and bilinear forms
38 // corresponding to the left-hand side and right-hand side of the
39 // discrete linear system. We also cover the explicit elimination
40 // of essential boundary conditions, static condensation, and the
41 // optional connection to the GLVis tool for visualization.
42 
43 #include "mfem.hpp"
44 #include <fstream>
45 #include <iostream>
46 
47 #ifndef MFEM_USE_SUPERLU
48 #error This example requires that MFEM is built with MFEM_USE_SUPERLU=YES
49 #endif
50 
51 using namespace std;
52 using namespace mfem;
53 
54 int main(int argc, char *argv[])
55 {
56  // 1. Initialize MPI.
57  int num_procs, myid;
58  MPI_Init(&argc, &argv);
59  MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
60  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
61 
62  // 2. Parse command-line options.
63  const char *mesh_file = "../../data/star.mesh";
64  int order = 1;
65  const char *device_config = "cpu";
66  bool visualization = true;
67  int slu_colperm = 4;
68  int slu_rowperm = 1;
69  int slu_iterref = 2;
70 
71  OptionsParser args(argc, argv);
72  args.AddOption(&mesh_file, "-m", "--mesh",
73  "Mesh file to use.");
74  args.AddOption(&order, "-o", "--order",
75  "Finite element order (polynomial degree) or -1 for"
76  " isoparametric space.");
77  args.AddOption(&device_config, "-d", "--device",
78  "Device configuration string, see Device::Configure().");
79  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
80  "--no-visualization",
81  "Enable or disable GLVis visualization.");
82  args.AddOption(&slu_colperm, "-cp", "--colperm",
83  "SuperLU Column Permutation Method: 0-NATURAL, 1-MMD-ATA "
84  "2-MMD_AT_PLUS_A, 3-COLAMD, 4-METIS_AT_PLUS_A, 5-PARMETIS "
85  "6-ZOLTAN");
86  args.AddOption(&slu_rowperm, "-rp", "--rowperm",
87  "SuperLU Row Permutation Method: 0-NOROWPERM, 1-LargeDiag");
88  args.AddOption(&slu_iterref, "-rp", "--rowperm",
89  "SuperLU Iterative Refinement: 0-NOREFINE, 1-Single, "
90  "2-Double, 3-Extra");
91 
92  args.Parse();
93  if (!args.Good())
94  {
95  if (myid == 0)
96  {
97  args.PrintUsage(cout);
98  }
99  MPI_Finalize();
100  return 1;
101  }
102  if (myid == 0)
103  {
104  args.PrintOptions(cout);
105  }
106 
107  // 3. Enable hardware devices such as GPUs, and programming models such as
108  // CUDA, OCCA, RAJA and OpenMP based on command line options.
109  Device device(device_config);
110  if (myid == 0) { device.Print(); }
111 
112  // 4. Read the (serial) mesh from the given mesh file on all processors. We
113  // can handle triangular, quadrilateral, tetrahedral, hexahedral, surface
114  // and volume meshes with the same code.
115  Mesh mesh(mesh_file, 1, 1);
116  int dim = mesh.Dimension();
117 
118  // 5. Refine the serial mesh on all processors to increase the resolution. In
119  // this example we do 'ref_levels' of uniform refinement. We choose
120  // 'ref_levels' to be the largest number that gives a final mesh with no
121  // more than 1,000 elements.
122  {
123  int ref_levels =
124  (int)floor(log(1000./mesh.GetNE())/log(2.)/dim);
125  for (int l = 0; l < ref_levels; l++)
126  {
127  mesh.UniformRefinement();
128  }
129  }
130 
131  // 6. Define a parallel mesh by a partitioning of the serial mesh. Refine
132  // this mesh further in parallel to increase the resolution. Once the
133  // parallel mesh is defined, the serial mesh can be deleted.
134  ParMesh pmesh(MPI_COMM_WORLD, mesh);
135  mesh.Clear();
136  {
137  int par_ref_levels = 2;
138  for (int l = 0; l < par_ref_levels; l++)
139  {
140  pmesh.UniformRefinement();
141  }
142  }
143 
144  // 7. Define a parallel finite element space on the parallel mesh. Here we
145  // use continuous Lagrange finite elements of the specified order. If
146  // order < 1, we instead use an isoparametric/isogeometric space.
148  bool delete_fec;
149  if (order > 0)
150  {
151  fec = new H1_FECollection(order, dim);
152  delete_fec = true;
153  }
154  else if (pmesh.GetNodes())
155  {
156  fec = pmesh.GetNodes()->OwnFEC();
157  delete_fec = false;
158  if (myid == 0)
159  {
160  cout << "Using isoparametric FEs: " << fec->Name() << endl;
161  }
162  }
163  else
164  {
165  fec = new H1_FECollection(order = 1, dim);
166  delete_fec = true;
167  }
168  ParFiniteElementSpace fespace(&pmesh, fec);
169  HYPRE_Int size = fespace.GlobalTrueVSize();
170  if (myid == 0)
171  {
172  cout << "Number of finite element unknowns: " << size << endl;
173  }
174 
175  // 8. Determine the list of true (i.e. parallel conforming) essential
176  // boundary dofs. In this example, the boundary conditions are defined
177  // by marking all the boundary attributes from the mesh as essential
178  // (Dirichlet) and converting them to a list of true dofs.
179  Array<int> ess_tdof_list;
180  if (pmesh.bdr_attributes.Size())
181  {
182  Array<int> ess_bdr(pmesh.bdr_attributes.Max());
183  ess_bdr = 1;
184  fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
185  }
186 
187  // 9. Set up the parallel linear form b(.) which corresponds to the
188  // right-hand side of the FEM linear system, which in this case is
189  // (1,phi_i) where phi_i are the basis functions in fespace.
190  ParLinearForm b(&fespace);
191  ConstantCoefficient one(1.0);
193  b.Assemble();
194 
195  // 10. Define the solution vector x as a parallel finite element grid function
196  // corresponding to fespace. Initialize x with initial guess of zero,
197  // which satisfies the boundary conditions.
198  ParGridFunction x(&fespace);
199  x = 0.0;
200 
201  // 11. Set up the parallel bilinear form a(.,.) on the finite element space
202  // corresponding to the Laplacian operator -Delta, by adding the Diffusion
203  // domain integrator.
204  ParBilinearForm a(&fespace);
206 
207  // 12. Assemble the parallel bilinear form and the corresponding linear
208  // system, applying any necessary transformations such as: parallel
209  // assembly, eliminating boundary conditions, applying conforming
210  // constraints for non-conforming AMR, static condensation, etc.
211  a.Assemble();
212 
213  OperatorPtr A;
214  Vector B, X;
215  a.FormLinearSystem(ess_tdof_list, x, b, A, X, B);
216 
217  // 13. Solve the linear system A X = B utilizing SuperLU.
218  SuperLUSolver *superlu = new SuperLUSolver(MPI_COMM_WORLD);
219  Operator *SLU_A = new SuperLURowLocMatrix(*A.As<HypreParMatrix>());
220  superlu->SetPrintStatistics(true);
221  superlu->SetSymmetricPattern(false);
222 
223  if (slu_colperm == 0)
224  {
226  }
227  else if (slu_colperm == 1)
228  {
230  }
231  else if (slu_colperm == 2)
232  {
234  }
235  else if (slu_colperm == 3)
236  {
238  }
239  else if (slu_colperm == 4)
240  {
242  }
243  else if (slu_colperm == 5)
244  {
246  }
247  else if (slu_colperm == 6)
248  {
250  }
251 
252  if (slu_rowperm == 0)
253  {
255  }
256  else if (slu_rowperm == 1)
257  {
258 #ifdef MFEM_USE_SUPERLU5
260 #else
262 #endif
263  }
264 
265  if (slu_iterref == 0)
266  {
268  }
269  else if (slu_iterref == 1)
270  {
272  }
273  else if (slu_iterref == 2)
274  {
276  }
277  else if (slu_iterref == 3)
278  {
280  }
281 
282  superlu->SetOperator(*SLU_A);
283  superlu->SetPrintStatistics(true);
284  superlu->Mult(B, X);
285 
286  // 14. Recover the parallel grid function corresponding to X. This is the
287  // local finite element solution on each processor.
288  a.RecoverFEMSolution(X, b, x);
289 
290  // 15. Save the refined mesh and the solution in parallel. This output can
291  // be viewed later using GLVis: "glvis -np <np> -m mesh -g sol".
292  {
293  ostringstream mesh_name, sol_name;
294  mesh_name << "mesh." << setfill('0') << setw(6) << myid;
295  sol_name << "sol." << setfill('0') << setw(6) << myid;
296 
297  ofstream mesh_ofs(mesh_name.str().c_str());
298  mesh_ofs.precision(8);
299  pmesh.Print(mesh_ofs);
300 
301  ofstream sol_ofs(sol_name.str().c_str());
302  sol_ofs.precision(8);
303  x.Save(sol_ofs);
304  }
305 
306  // 16. Send the solution by socket to a GLVis server.
307  if (visualization)
308  {
309  char vishost[] = "localhost";
310  int visport = 19916;
311  socketstream sol_sock(vishost, visport);
312  sol_sock << "parallel " << num_procs << " " << myid << "\n";
313  sol_sock.precision(8);
314  sol_sock << "solution\n" << pmesh << x << flush;
315  }
316 
317  // 17. Free the used memory.
318  if (delete_fec)
319  {
320  delete fec;
321  }
322  MPI_Finalize();
323 
324  return 0;
325 }
int Size() const
Return the logical size of the array.
Definition: array.hpp:124
Class for domain integration L(v) := (f, v)
Definition: lininteg.hpp:93
virtual void GetEssentialTrueDofs(const Array< int > &bdr_attr_is_ess, Array< int > &ess_tdof_list, int component=-1)
Definition: pfespace.cpp:775
OpType * As() const
Return the Operator pointer statically cast to a specified OpType. Similar to the method Get()...
Definition: handle.hpp:96
void Mult(const Vector &x, Vector &y) const
Operator application: y=A(x).
Definition: superlu.cpp:471
A coefficient that is constant across space and time.
Definition: coefficient.hpp:78
virtual void FormLinearSystem(const Array< int > &ess_tdof_list, Vector &x, Vector &b, OperatorHandle &A, Vector &X, Vector &B, int copy_interior=0)
Form the linear system A X = B, corresponding to this bilinear form and the linear form b(...
void Assemble()
Assembles the linear form i.e. sums over all domain/bdr integrators.
Definition: linearform.cpp:79
Pointer to an Operator of a specified type.
Definition: handle.hpp:33
void SetRowPermutation(superlu::RowPerm row_perm, Array< int > *perm=NULL)
Definition: superlu.cpp:349
int GetNE() const
Returns number of elements.
Definition: mesh.hpp:737
virtual void Save(std::ostream &out) const
Definition: pgridfunc.cpp:819
void Print(std::ostream &out=mfem::out)
Print the configuration of the MFEM virtual device object.
Definition: device.cpp:261
Abstract parallel finite element space.
Definition: pfespace.hpp:28
int main(int argc, char *argv[])
Definition: ex1.cpp:66
void SetSymmetricPattern(bool sym)
Definition: superlu.cpp:420
void SetOperator(const Operator &op)
Set/update the solver for the given operator.
Definition: superlu.cpp:576
Class for parallel linear form.
Definition: plinearform.hpp:26
void SetIterativeRefine(superlu::IterRefine iter_ref)
Definition: superlu.cpp:386
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 SetColumnPermutation(superlu::ColPerm col_perm)
Definition: superlu.cpp:340
double b
Definition: lissajous.cpp:42
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:8382
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
HYPRE_Int GlobalTrueVSize() const
Definition: pfespace.hpp:251
void Assemble(int skip_zeros=1)
Assemble the local matrix.
virtual void Print(std::ostream &out=mfem::out) const
Definition: pmesh.cpp:4169
void SetPrintStatistics(bool print_stat)
Definition: superlu.cpp:322
int Dimension() const
Definition: mesh.hpp:788
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:434
void AddDomainIntegrator(LinearFormIntegrator *lfi)
Adds new Domain Integrator. Assumes ownership of lfi.
Definition: linearform.cpp:39
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
Definition: mesh.hpp:201
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
virtual void RecoverFEMSolution(const Vector &X, const Vector &b, Vector &x)
double a
Definition: lissajous.cpp:41
virtual const char * Name() const
Definition: fe_coll.hpp:61
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:304
Class for parallel bilinear form.
void Clear()
Clear the contents of the Mesh.
Definition: mesh.hpp:719
Vector data type.
Definition: vector.hpp:51
void GetNodes(Vector &node_coord) const
Definition: mesh.cpp:6603
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:159
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:118
Abstract operator.
Definition: operator.hpp:24
Wrapper for hypre&#39;s ParCSR matrix class.
Definition: hypre.hpp:181
Class for parallel meshes.
Definition: pmesh.hpp:32
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:145