MFEM  v4.6.0
Finite element discretization library
ex1.cpp
Go to the documentation of this file.
1 // MFEM Example 1
2 // PUMI Modification
3 //
4 // Compile with: make ex1
5 //
6 // Sample runs:
7 // ex1 -m ../../data/pumi/serial/Kova.smb -p ../../data/pumi/geom/Kova.dmg
8 //
9 // Note: Example models + meshes for the PUMI examples can be downloaded
10 // from github.com/mfem/data/pumi. After downloading we recommend
11 // creating a symbolic link to the above directory in ../../data.
12 //
13 // Description: This example code demonstrates the use of MFEM to define a
14 // simple finite element discretization of the Laplace problem
15 // -Delta u = 1 with homogeneous Dirichlet boundary conditions.
16 // Specifically, we discretize using a FE space of the specified
17 // order, or if order < 1 using an isoparametric/isogeometric
18 // space (i.e. quadratic for quadratic curvilinear mesh, NURBS for
19 // NURBS mesh, etc.)
20 //
21 // The example highlights the use of mesh refinement, finite
22 // element grid functions, as well as linear and bilinear forms
23 // corresponding to the left-hand side and right-hand side of the
24 // discrete linear system. We also cover the explicit elimination
25 // of essential boundary conditions, static condensation, and the
26 // optional connection to the GLVis tool for visualization.
27 //
28 // This PUMI modification demonstrates how PUMI's API can be used
29 // to load a PUMI mesh classified on a geometric model and then
30 // convert it to the MFEM mesh format. The inputs are a Parasolid
31 // model, "*.xmt_txt" and a SCOREC mesh "*.smb". The option "-o"
32 // is used for the Finite Element order and "-go" is used for the
33 // geometry order. Note that they can be used independently, i.e.
34 // "-o 8 -go 3" solves for 8th order FE on a third order geometry.
35 //
36 // NOTE: Model/Mesh files for this example are in the (large) data file
37 // repository of MFEM here https://github.com/mfem/data under the
38 // folder named "pumi", which consists of the following sub-folders:
39 // a) geom --> model files
40 // b) parallel --> parallel pumi mesh files
41 // c) serial --> serial pumi mesh files
42 
43 #include "mfem.hpp"
44 #include <fstream>
45 #include <iostream>
46 
47 #ifdef MFEM_USE_SIMMETRIX
48 #include <SimUtil.h>
49 #include <gmi_sim.h>
50 #endif
51 #include <apfMDS.h>
52 #include <gmi_null.h>
53 #include <PCU.h>
54 #include <apfConvert.h>
55 #include <gmi_mesh.h>
56 #include <crv.h>
57 
58 #ifndef MFEM_USE_PUMI
59 #error This example requires that MFEM is built with MFEM_USE_PUMI=YES
60 #endif
61 
62 using namespace std;
63 using namespace mfem;
64 
65 int main(int argc, char *argv[])
66 {
67  // 1. Initialize MPI (required by PUMI) and HYPRE.
68  Mpi::Init(argc, argv);
69  int num_procs = Mpi::WorldSize();
70  int myid = Mpi::WorldRank();
71  Hypre::Init();
72 
73  // 2. Parse command-line options.
74  const char *mesh_file = "../../data/pumi/serial/Kova.smb";
75 #ifdef MFEM_USE_SIMMETRIX
76  const char *model_file = "../../data/pumi/geom/Kova.x_t";
77 #else
78  const char *model_file = "../../data/pumi/geom/Kova.dmg";
79 #endif
80  int order = 1;
81  bool static_cond = false;
82  bool visualization = 1;
83  int geom_order = 1;
84 
85  OptionsParser args(argc, argv);
86  args.AddOption(&mesh_file, "-m", "--mesh",
87  "Mesh file to use.");
88  args.AddOption(&order, "-o", "--order",
89  "Finite element order (polynomial degree) or -1 for"
90  " isoparametric space.");
91  args.AddOption(&static_cond, "-sc", "--static-condensation", "-no-sc",
92  "--no-static-condensation", "Enable static condensation.");
93  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
94  "--no-visualization",
95  "Enable or disable GLVis visualization.");
96  args.AddOption(&model_file, "-p", "--parasolid",
97  "Parasolid model to use.");
98  args.AddOption(&geom_order, "-go", "--geometry_order",
99  "Geometric order of the model");
100  args.Parse();
101  if (!args.Good())
102  {
103  if (myid == 0)
104  {
105  args.PrintUsage(cout);
106  }
107  return 1;
108  }
109  if (myid == 0)
110  {
111  args.PrintOptions(cout);
112  }
113 
114  // 3. Read the SCOREC Mesh.
115  PCU_Comm_Init();
116 #ifdef MFEM_USE_SIMMETRIX
117  Sim_readLicenseFile(0);
118  gmi_sim_start();
119  gmi_register_sim();
120 #endif
121  gmi_register_mesh();
122 
123  apf::Mesh2* pumi_mesh;
124  pumi_mesh = apf::loadMdsMesh(model_file, mesh_file);
125 
126  // 4. Increase the geometry order if necessary.
127  if (geom_order > 1)
128  {
129  crv::BezierCurver bc(pumi_mesh, geom_order, 2);
130  bc.run();
131  }
132 
133  pumi_mesh->verify();
134 
135  // 5. Create the MFEM mesh object from the PUMI mesh. We can handle
136  // triangular and tetrahedral meshes. Other inputs are the same as the
137  // MFEM default constructor.
138  Mesh *mesh = new PumiMesh(pumi_mesh, 1, 1);
139  int dim = mesh->Dimension();
140 
141  // 6. Refine the mesh to increase the resolution. In this example we do
142  // 'ref_levels' of uniform refinement. We choose 'ref_levels' to be the
143  // largest number that gives a final mesh with no more than 50,000
144  // elements.
145  {
146  int ref_levels =
147  (int)floor(log(50000./mesh->GetNE())/log(2.)/dim);
148  for (int l = 0; l < ref_levels; l++)
149  {
150  mesh->UniformRefinement();
151  }
152  }
153 
154  // 7. Define a finite element space on the mesh. Here we use continuous
155  // Lagrange finite elements of the specified order. If order < 1, we
156  // instead use an isoparametric/isogeometric space.
158  if (order > 0)
159  {
160  fec = new H1_FECollection(order, dim);
161  }
162  else if (mesh->GetNodes())
163  {
164  fec = mesh->GetNodes()->OwnFEC();
165  cout << "Using isoparametric FEs: " << fec->Name() << endl;
166  }
167  else
168  {
169  fec = new H1_FECollection(order = 1, dim);
170  }
171  FiniteElementSpace *fespace = new FiniteElementSpace(mesh, fec);
172  cout << "Number of finite element unknowns: "
173  << fespace->GetTrueVSize() << endl;
174 
175  // 8. Determine the list of true (i.e. conforming) essential boundary dofs.
176  // In this example, the boundary conditions are defined by marking all
177  // the boundary attributes from the mesh as essential (Dirichlet) and
178  // converting them to a list of true dofs.
179  Array<int> ess_tdof_list;
180  if (mesh->bdr_attributes.Size())
181  {
182  Array<int> ess_bdr(mesh->bdr_attributes.Max());
183  ess_bdr = 1;
184  fespace->GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
185  }
186 
187  // 9. Set up the linear form b(.) which corresponds to the right-hand side of
188  // the FEM linear system, which in this case is (1,phi_i) where phi_i are
189  // the basis functions in the finite element fespace.
190  LinearForm *b = new LinearForm(fespace);
191  ConstantCoefficient one(1.0);
192  b->AddDomainIntegrator(new DomainLFIntegrator(one));
193  b->Assemble();
194 
195  // 10. Define the solution vector x as a finite element grid function
196  // corresponding to fespace. Initialize x with initial guess of zero,
197  // which satisfies the boundary conditions.
198  GridFunction x(fespace);
199  x = 0.0;
200 
201  // 11. Set up the bilinear form a(.,.) on the finite element space
202  // corresponding to the Laplacian operator -Delta, by adding the
203  // Diffusion domain integrator.
204  BilinearForm *a = new BilinearForm(fespace);
205  a->AddDomainIntegrator(new DiffusionIntegrator(one));
206 
207  // 12. Assemble the bilinear form and the corresponding linear system,
208  // applying any necessary transformations such as: eliminating boundary
209  // conditions, applying conforming constraints for non-conforming AMR,
210  // static condensation, etc.
211  if (static_cond) { a->EnableStaticCondensation(); }
212  a->Assemble();
213 
214  SparseMatrix A;
215  Vector B, X;
216  a->FormLinearSystem(ess_tdof_list, x, *b, A, X, B);
217 
218  cout << "Size of linear system: " << A.Height() << endl;
219 
220 #ifndef MFEM_USE_SUITESPARSE
221  // 13. Define a simple symmetric Gauss-Seidel preconditioner and use it to
222  // solve the system A X = B with PCG.
223  GSSmoother M(A);
224  PCG(A, M, B, X, 1, 200, 1e-12, 0.0);
225 #else
226  // 13. If MFEM was compiled with SuiteSparse, use UMFPACK to solve the system.
227  UMFPackSolver umf_solver;
228  umf_solver.Control[UMFPACK_ORDERING] = UMFPACK_ORDERING_METIS;
229  umf_solver.SetOperator(A);
230  umf_solver.Mult(B, X);
231 #endif
232 
233  // 14. Recover the solution as a finite element grid function.
234  a->RecoverFEMSolution(X, *b, x);
235 
236  // 15. Save the refined mesh and the solution. This output can be viewed later
237  // using GLVis: "glvis -m refined.mesh -g sol.gf".
238  ofstream mesh_ofs("refined.mesh");
239  mesh_ofs.precision(8);
240  mesh->Print(mesh_ofs);
241  ofstream sol_ofs("sol.gf");
242  sol_ofs.precision(8);
243  x.Save(sol_ofs);
244 
245  // 16. Send the solution by socket to a GLVis server.
246  if (visualization)
247  {
248  char vishost[] = "localhost";
249  int visport = 19916;
250  socketstream sol_sock(vishost, visport);
251  sol_sock.precision(8);
252  sol_sock << "solution\n" << *mesh << x << flush;
253  }
254 
255  // 17. Free the used memory.
256  delete a;
257  delete b;
258  delete fespace;
259  if (order > 0) { delete fec; }
260  delete mesh;
261 
262  pumi_mesh->destroyNative();
263  apf::destroyMesh(pumi_mesh);
264  PCU_Comm_Free();
265 #ifdef MFEM_USE_SIMMETRIX
266  gmi_sim_stop();
267  Sim_unregisterAllKeys();
268 #endif
269 
270  return 0;
271 }
Class for domain integration L(v) := (f, v)
Definition: lininteg.hpp:108
int visport
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:30
A coefficient that is constant across space and time.
Definition: coefficient.hpp:84
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:331
int Dimension() const
Dimension of the reference space used within the elements.
Definition: mesh.hpp:1020
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:462
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
Definition: array.cpp:68
virtual void GetEssentialTrueDofs(const Array< int > &bdr_attr_is_ess, Array< int > &ess_tdof_list, int component=-1)
Get a list of essential true dofs, ess_tdof_list, corresponding to the boundary attributes marked in ...
Definition: fespace.cpp:587
Base class for PUMI meshes.
Definition: pumi.hpp:44
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:159
STL namespace.
int main(int argc, char *argv[])
Definition: ex1.cpp:74
Data type for Gauss-Seidel smoother of sparse matrix.
Direct sparse solver using UMFPACK.
Definition: solvers.hpp:1070
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Definition: optparser.cpp:151
char vishost[]
Data type sparse matrix.
Definition: sparsemat.hpp:50
double b
Definition: lissajous.cpp:42
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:10232
virtual const char * Name() const
Definition: fe_coll.hpp:80
void PCG(const Operator &A, Solver &B, const Vector &b, Vector &x, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
Preconditioned conjugate gradient method. (tolerances are squared)
Definition: solvers.cpp:913
virtual int GetTrueVSize() const
Return the number of vector true (conforming) dofs.
Definition: fespace.hpp:712
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
Definition: mesh.hpp:275
double Control[UMFPACK_CONTROL]
Definition: solvers.hpp:1081
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:219
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 Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:3194
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
Definition: operator.hpp:66
int GetNE() const
Returns number of elements.
Definition: mesh.hpp:1086
double a
Definition: lissajous.cpp:41
A "square matrix" operator for the associated FE space and BLFIntegrators The sum of all the BLFInteg...
int dim
Definition: ex24.cpp:53
int Size() const
Return the logical size of the array.
Definition: array.hpp:141
Vector data type.
Definition: vector.hpp:58
virtual void Print(std::ostream &os=mfem::out) const
Definition: mesh.hpp:2011
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:259
Vector with associated FE space and LinearFormIntegrators.
Definition: linearform.hpp:24
void GetNodes(Vector &node_coord) const
Definition: mesh.cpp:8302
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
Definition: gridfunc.cpp:3696
virtual void SetOperator(const Operator &op)
Factorize the given Operator op which must be a SparseMatrix.
Definition: solvers.cpp:3099