MFEM  v3.3.2
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
ex16p.cpp
Go to the documentation of this file.
1 // MFEM Example 16 - Parallel Version
2 //
3 // Compile with: make ex16p
4 //
5 // Sample runs: mpirun -np 4 ex16p
6 // mpirun -np 4 ex16p -m ../data/inline-tri.mesh
7 // mpirun -np 4 ex16p -m ../data/disc-nurbs.mesh -tf 2
8 // mpirun -np 4 ex16p -s 1 -a 0.0 -k 1.0
9 // mpirun -np 4 ex16p -s 2 -a 1.0 -k 0.0
10 // mpirun -np 8 ex16p -s 3 -a 0.5 -k 0.5 -o 4
11 // mpirun -np 4 ex16p -s 14 -dt 1.0e-4 -tf 4.0e-2 -vs 40
12 // mpirun -np 16 ex16p -m ../data/fichera-q2.mesh
13 // mpirun -np 16 ex16p -m ../data/escher-p2.mesh
14 // mpirun -np 8 ex16p -m ../data/beam-tet.mesh -tf 10 -dt 0.1
15 // mpirun -np 4 ex16p -m ../data/amr-quad.mesh -o 4 -rs 0 -rp 0
16 // mpirun -np 4 ex16p -m ../data/amr-hex.mesh -o 2 -rs 0 -rp 0
17 //
18 // Description: This example solves a time dependent nonlinear heat equation
19 // problem of the form du/dt = C(u), with a non-linear diffusion
20 // operator C(u) = \nabla \cdot (\kappa + \alpha u) \nabla u.
21 //
22 // The example demonstrates the use of nonlinear operators (the
23 // class ConductionOperator defining C(u)), as well as their
24 // implicit time integration. Note that implementing the method
25 // ConductionOperator::ImplicitSolve is the only requirement for
26 // high-order implicit (SDIRK) time integration.
27 //
28 // We recommend viewing examples 2, 9 and 10 before viewing this
29 // example.
30 
31 #include "mfem.hpp"
32 #include <fstream>
33 #include <iostream>
34 
35 using namespace std;
36 using namespace mfem;
37 
38 /** After spatial discretization, the conduction model can be written as:
39  *
40  * du/dt = M^{-1}(-Ku)
41  *
42  * where u is the vector representing the temperature, M is the mass matrix,
43  * and K is the diffusion operator with diffusivity depending on u:
44  * (\kappa + \alpha u).
45  *
46  * Class ConductionOperator represents the right-hand side of the above ODE.
47  */
48 class ConductionOperator : public TimeDependentOperator
49 {
50 protected:
51  ParFiniteElementSpace &fespace;
52  Array<int> ess_tdof_list; // this list remains empty for pure Neumann b.c.
53 
54  ParBilinearForm *M;
55  ParBilinearForm *K;
56 
57  HypreParMatrix Mmat;
58  HypreParMatrix Kmat;
59  HypreParMatrix *T; // T = M + dt K
60  double current_dt;
61 
62  CGSolver M_solver; // Krylov solver for inverting the mass matrix M
63  HypreSmoother M_prec; // Preconditioner for the mass matrix M
64 
65  CGSolver T_solver; // Implicit solver for T = M + dt K
66  HypreSmoother T_prec; // Preconditioner for the implicit solver
67 
68  double alpha, kappa;
69 
70  mutable Vector z; // auxiliary vector
71 
72 public:
73  ConductionOperator(ParFiniteElementSpace &f, double alpha, double kappa,
74  const Vector &u);
75 
76  virtual void Mult(const Vector &u, Vector &du_dt) const;
77  /** Solve the Backward-Euler equation: k = f(u + dt*k, t), for the unknown k.
78  This is the only requirement for high-order SDIRK implicit integration.*/
79  virtual void ImplicitSolve(const double dt, const Vector &u, Vector &k);
80 
81  /// Update the diffusion BilinearForm K using the given true-dof vector `u`.
82  void SetParameters(const Vector &u);
83 
84  virtual ~ConductionOperator();
85 };
86 
87 double InitialTemperature(const Vector &x);
88 
89 int main(int argc, char *argv[])
90 {
91  // 1. Initialize MPI.
92  int num_procs, myid;
93  MPI_Init(&argc, &argv);
94  MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
95  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
96 
97  // 2. Parse command-line options.
98  const char *mesh_file = "../data/star.mesh";
99  int ser_ref_levels = 2;
100  int par_ref_levels = 1;
101  int order = 2;
102  int ode_solver_type = 3;
103  double t_final = 0.5;
104  double dt = 1.0e-2;
105  double alpha = 1.0e-2;
106  double kappa = 0.5;
107  bool visualization = true;
108  bool visit = false;
109  int vis_steps = 5;
110 
111  int precision = 8;
112  cout.precision(precision);
113 
114  OptionsParser args(argc, argv);
115  args.AddOption(&mesh_file, "-m", "--mesh",
116  "Mesh file to use.");
117  args.AddOption(&ser_ref_levels, "-rs", "--refine-serial",
118  "Number of times to refine the mesh uniformly in serial.");
119  args.AddOption(&par_ref_levels, "-rp", "--refine-parallel",
120  "Number of times to refine the mesh uniformly in parallel.");
121  args.AddOption(&order, "-o", "--order",
122  "Order (degree) of the finite elements.");
123  args.AddOption(&ode_solver_type, "-s", "--ode-solver",
124  "ODE solver: 1 - Backward Euler, 2 - SDIRK2, 3 - SDIRK3,\n\t"
125  "\t 11 - Forward Euler, 12 - RK2, 13 - RK3 SSP, 14 - RK4.");
126  args.AddOption(&t_final, "-tf", "--t-final",
127  "Final time; start time is 0.");
128  args.AddOption(&dt, "-dt", "--time-step",
129  "Time step.");
130  args.AddOption(&alpha, "-a", "--alpha",
131  "Alpha coefficient.");
132  args.AddOption(&kappa, "-k", "--kappa",
133  "Kappa coefficient offset.");
134  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
135  "--no-visualization",
136  "Enable or disable GLVis visualization.");
137  args.AddOption(&visit, "-visit", "--visit-datafiles", "-no-visit",
138  "--no-visit-datafiles",
139  "Save data files for VisIt (visit.llnl.gov) visualization.");
140  args.AddOption(&vis_steps, "-vs", "--visualization-steps",
141  "Visualize every n-th timestep.");
142  args.Parse();
143  if (!args.Good())
144  {
145  args.PrintUsage(cout);
146  MPI_Finalize();
147  return 1;
148  }
149 
150  if (myid == 0)
151  {
152  args.PrintOptions(cout);
153  }
154 
155  // 3. Read the serial mesh from the given mesh file on all processors. We can
156  // handle triangular, quadrilateral, tetrahedral and hexahedral meshes
157  // with the same code.
158  Mesh *mesh = new Mesh(mesh_file, 1, 1);
159  int dim = mesh->Dimension();
160 
161  // 4. Define the ODE solver used for time integration. Several implicit
162  // singly diagonal implicit Runge-Kutta (SDIRK) methods, as well as
163  // explicit Runge-Kutta methods are available.
164  ODESolver *ode_solver;
165  switch (ode_solver_type)
166  {
167  // Implicit L-stable methods
168  case 1: ode_solver = new BackwardEulerSolver; break;
169  case 2: ode_solver = new SDIRK23Solver(2); break;
170  case 3: ode_solver = new SDIRK33Solver; break;
171  // Explicit methods
172  case 11: ode_solver = new ForwardEulerSolver; break;
173  case 12: ode_solver = new RK2Solver(0.5); break; // midpoint method
174  case 13: ode_solver = new RK3SSPSolver; break;
175  case 14: ode_solver = new RK4Solver; break;
176  // Implicit A-stable methods (not L-stable)
177  case 22: ode_solver = new ImplicitMidpointSolver; break;
178  case 23: ode_solver = new SDIRK23Solver; break;
179  case 24: ode_solver = new SDIRK34Solver; break;
180  default:
181  cout << "Unknown ODE solver type: " << ode_solver_type << '\n';
182  return 3;
183  }
184 
185  // 5. Refine the mesh in serial to increase the resolution. In this example
186  // we do 'ser_ref_levels' of uniform refinement, where 'ser_ref_levels' is
187  // a command-line parameter.
188  for (int lev = 0; lev < ser_ref_levels; lev++)
189  {
190  mesh->UniformRefinement();
191  }
192 
193  // 6. Define a parallel mesh by a partitioning of the serial mesh. Refine
194  // this mesh further in parallel to increase the resolution. Once the
195  // parallel mesh is defined, the serial mesh can be deleted.
196  ParMesh *pmesh = new ParMesh(MPI_COMM_WORLD, *mesh);
197  delete mesh;
198  for (int lev = 0; lev < par_ref_levels; lev++)
199  {
200  pmesh->UniformRefinement();
201  }
202 
203  // 7. Define the vector finite element space representing the current and the
204  // initial temperature, u_ref.
205  H1_FECollection fe_coll(order, dim);
206  ParFiniteElementSpace fespace(pmesh, &fe_coll);
207 
208  int fe_size = fespace.GlobalTrueVSize();
209  if (myid == 0)
210  {
211  cout << "Number of temperature unknowns: " << fe_size << endl;
212  }
213 
214  ParGridFunction u_gf(&fespace);
215 
216  // 8. Set the initial conditions for u. All boundaries are considered
217  // natural.
219  u_gf.ProjectCoefficient(u_0);
220  Vector u;
221  u_gf.GetTrueDofs(u);
222 
223  // 9. Initialize the conduction operator and the VisIt visualization.
224  ConductionOperator oper(fespace, alpha, kappa, u);
225 
226  u_gf.SetFromTrueDofs(u);
227  {
228  ostringstream mesh_name, sol_name;
229  mesh_name << "ex16-mesh." << setfill('0') << setw(6) << myid;
230  sol_name << "ex16-init." << setfill('0') << setw(6) << myid;
231  ofstream omesh(mesh_name.str().c_str());
232  omesh.precision(precision);
233  pmesh->Print(omesh);
234  ofstream osol(sol_name.str().c_str());
235  osol.precision(precision);
236  u_gf.Save(osol);
237  }
238 
239  VisItDataCollection visit_dc("Example16-Parallel", pmesh);
240  visit_dc.RegisterField("temperature", &u_gf);
241  if (visit)
242  {
243  visit_dc.SetCycle(0);
244  visit_dc.SetTime(0.0);
245  visit_dc.Save();
246  }
247 
248  socketstream sout;
249  if (visualization)
250  {
251  char vishost[] = "localhost";
252  int visport = 19916;
253  sout.open(vishost, visport);
254  sout << "parallel " << num_procs << " " << myid << endl;
255  int good = sout.good(), all_good;
256  MPI_Allreduce(&good, &all_good, 1, MPI_INT, MPI_MIN, pmesh->GetComm());
257  if (!all_good)
258  {
259  sout.close();
260  visualization = false;
261  if (myid == 0)
262  {
263  cout << "Unable to connect to GLVis server at "
264  << vishost << ':' << visport << endl;
265  cout << "GLVis visualization disabled.\n";
266  }
267  }
268  else
269  {
270  sout.precision(precision);
271  sout << "solution\n" << *pmesh << u_gf;
272  sout << "pause\n";
273  sout << flush;
274  if (myid == 0)
275  {
276  cout << "GLVis visualization paused."
277  << " Press space (in the GLVis window) to resume it.\n";
278  }
279  }
280  }
281 
282  // 10. Perform time-integration (looping over the time iterations, ti, with a
283  // time-step dt).
284  ode_solver->Init(oper);
285  double t = 0.0;
286 
287  bool last_step = false;
288  for (int ti = 1; !last_step; ti++)
289  {
290  if (t + dt >= t_final - dt/2)
291  {
292  last_step = true;
293  }
294 
295  ode_solver->Step(u, t, dt);
296 
297  if (last_step || (ti % vis_steps) == 0)
298  {
299  if (myid == 0)
300  {
301  cout << "step " << ti << ", t = " << t << endl;
302  }
303 
304  u_gf.SetFromTrueDofs(u);
305  if (visualization)
306  {
307  sout << "parallel " << num_procs << " " << myid << "\n";
308  sout << "solution\n" << *pmesh << u_gf << flush;
309  }
310 
311  if (visit)
312  {
313  visit_dc.SetCycle(ti);
314  visit_dc.SetTime(t);
315  visit_dc.Save();
316  }
317  }
318  oper.SetParameters(u);
319  }
320 
321  // 11. Save the final solution in parallel. This output can be viewed later
322  // using GLVis: "glvis -np <np> -m ex16-mesh -g ex16-final".
323  {
324  ostringstream sol_name;
325  sol_name << "ex16-final." << setfill('0') << setw(6) << myid;
326  ofstream osol(sol_name.str().c_str());
327  osol.precision(precision);
328  u_gf.Save(osol);
329  }
330 
331  // 12. Free the used memory.
332  delete ode_solver;
333  delete pmesh;
334 
335  MPI_Finalize();
336 
337  return 0;
338 }
339 
340 ConductionOperator::ConductionOperator(ParFiniteElementSpace &f, double al,
341  double kap, const Vector &u)
342  : TimeDependentOperator(f.GetTrueVSize(), 0.0), fespace(f), M(NULL), K(NULL),
343  T(NULL), current_dt(0.0),
344  M_solver(f.GetComm()), T_solver(f.GetComm()), z(height)
345 {
346  const double rel_tol = 1e-8;
347 
348  M = new ParBilinearForm(&fespace);
349  M->AddDomainIntegrator(new MassIntegrator());
350  M->Assemble(0); // keep sparsity pattern of M and K the same
351  M->FormSystemMatrix(ess_tdof_list, Mmat);
352 
353  M_solver.iterative_mode = false;
354  M_solver.SetRelTol(rel_tol);
355  M_solver.SetAbsTol(0.0);
356  M_solver.SetMaxIter(100);
357  M_solver.SetPrintLevel(0);
358  M_prec.SetType(HypreSmoother::Jacobi);
359  M_solver.SetPreconditioner(M_prec);
360  M_solver.SetOperator(Mmat);
361 
362  alpha = al;
363  kappa = kap;
364 
365  T_solver.iterative_mode = false;
366  T_solver.SetRelTol(rel_tol);
367  T_solver.SetAbsTol(0.0);
368  T_solver.SetMaxIter(100);
369  T_solver.SetPrintLevel(0);
370  T_solver.SetPreconditioner(T_prec);
371 
372  SetParameters(u);
373 }
374 
375 void ConductionOperator::Mult(const Vector &u, Vector &du_dt) const
376 {
377  // Compute:
378  // du_dt = M^{-1}*-K(u)
379  // for du_dt
380  Kmat.Mult(u, z);
381  z.Neg(); // z = -z
382  M_solver.Mult(z, du_dt);
383 }
384 
385 void ConductionOperator::ImplicitSolve(const double dt,
386  const Vector &u, Vector &du_dt)
387 {
388  // Solve the equation:
389  // du_dt = M^{-1}*[-K(u + dt*du_dt)]
390  // for du_dt
391  if (!T)
392  {
393  T = Add(1.0, Mmat, dt, Kmat);
394  current_dt = dt;
395  T_solver.SetOperator(*T);
396  }
397  MFEM_VERIFY(dt == current_dt, ""); // SDIRK methods use the same dt
398  Kmat.Mult(u, z);
399  z.Neg();
400  T_solver.Mult(z, du_dt);
401 }
402 
403 void ConductionOperator::SetParameters(const Vector &u)
404 {
405  ParGridFunction u_alpha_gf(&fespace);
406  u_alpha_gf.SetFromTrueDofs(u);
407  for (int i = 0; i < u_alpha_gf.Size(); i++)
408  {
409  u_alpha_gf(i) = kappa + alpha*u_alpha_gf(i);
410  }
411 
412  delete K;
413  K = new ParBilinearForm(&fespace);
414 
415  GridFunctionCoefficient u_coeff(&u_alpha_gf);
416 
417  K->AddDomainIntegrator(new DiffusionIntegrator(u_coeff));
418  K->Assemble(0); // keep sparsity pattern of M and K the same
419  K->FormSystemMatrix(ess_tdof_list, Kmat);
420  delete T;
421  T = NULL; // re-compute T on the next ImplicitSolve
422 }
423 
424 ConductionOperator::~ConductionOperator()
425 {
426  delete T;
427  delete M;
428  delete K;
429 }
430 
431 double InitialTemperature(const Vector &x)
432 {
433  if (x.Norml2() < 0.5)
434  {
435  return 2.0;
436  }
437  else
438  {
439  return 1.0;
440  }
441 }
Conjugate gradient method.
Definition: solvers.hpp:111
double InitialTemperature(const Vector &x)
Definition: ex16.cpp:379
void SetCycle(int c)
Set time cycle (for time-dependent simulations)
virtual void Init(TimeDependentOperator &f)
Associate a TimeDependentOperator with the ODE solver.
Definition: ode.hpp:37
Base abstract class for time dependent operators.
Definition: operator.hpp:142
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:468
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:670
virtual void Step(Vector &x, double &t, double &dt)=0
Perform a time step from time t [in] to time t [out] based on the requested step size dt [in]...
Coefficient defined by a GridFunction. This coefficient is mesh dependent.
Abstract class for solving systems of ODEs: dx/dt = f(x,t)
Definition: ode.hpp:22
virtual void Save()
Save the collection and a VisIt root file.
virtual void Save(std::ostream &out) const
Definition: pgridfunc.cpp:361
Abstract parallel finite element space.
Definition: pfespace.hpp:31
virtual void ProjectCoefficient(Coefficient &coeff)
Definition: pgridfunc.cpp:275
Backward Euler ODE solver. L-stable.
Definition: ode.hpp:212
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
Definition: densemat.cpp:2942
int dim
Definition: ex3.cpp:47
virtual void SetFromTrueDofs(const Vector &tv)
Set the GridFunction from the given true-dof vector.
Definition: pgridfunc.hpp:130
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:6718
Data collection with VisIt I/O routines.
HYPRE_Int GlobalTrueVSize() const
Definition: pfespace.hpp:178
virtual void Print(std::ostream &out=mfem::out) const
Definition: pmesh.cpp:3399
Parallel smoothers in hypre.
Definition: hypre.hpp:504
int Dimension() const
Definition: mesh.hpp:641
void PrintUsage(std::ostream &out) const
Definition: optparser.cpp:434
void SetTime(double t)
Set physical time (for time-dependent simulations)
int main()
The classical explicit forth-order Runge-Kutta method, RK4.
Definition: ode.hpp:147
MPI_Comm GetComm() const
Definition: pmesh.hpp:117
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)
Definition: optparser.hpp:74
Third-order, strong stability preserving (SSP) Runge-Kutta method.
Definition: ode.hpp:134
virtual void RegisterField(const std::string &field_name, GridFunction *gf)
Add a grid function to the collection and update the root file.
Implicit midpoint method. A-stable, not L-stable.
Definition: ode.hpp:225
void PrintOptions(std::ostream &out) const
Definition: optparser.cpp:304
HypreParVector * GetTrueDofs() const
Returns the true dofs in a new HypreParVector.
Definition: pgridfunc.cpp:126
Class for parallel bilinear form.
int open(const char hostname[], int port)
const double alpha
Definition: ex15.cpp:337
class for C-function coefficient
double kappa
Definition: ex3.cpp:46
Vector data type.
Definition: vector.hpp:41
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:146
Class for parallel grid function.
Definition: pgridfunc.hpp:32
The classical forward Euler method.
Definition: ode.hpp:101
Wrapper for hypre&#39;s ParCSR matrix class.
Definition: hypre.hpp:175
Class for parallel meshes.
Definition: pmesh.hpp:29
bool Good() const
Definition: optparser.hpp:120