MFEM  v3.3
Finite element discretization library
sundials/ex9.cpp
Go to the documentation of this file.
1 // MFEM Example 9
2 // SUNDIALS Modification
3 //
4 // Compile with: make ex9
5 //
6 // Sample runs:
7 // ex9 -m ../../data/periodic-segment.mesh -p 0 -r 2 -s 11 -dt 0.005
8 // ex9 -m ../../data/periodic-square.mesh -p 1 -r 2 -s 12 -dt 0.005 -tf 9
9 // ex9 -m ../../data/periodic-hexagon.mesh -p 0 -r 2 -s 11 -dt 0.0018 -vs 25
10 // ex9 -m ../../data/periodic-hexagon.mesh -p 0 -r 2 -s 13 -dt 0.01 -vs 15
11 // ex9 -m ../../data/amr-quad.mesh -p 1 -r 2 -s 13 -dt 0.002 -tf 9
12 // ex9 -m ../../data/star-q3.mesh -p 1 -r 2 -s 13 -dt 0.005 -tf 9
13 // ex9 -m ../../data/disc-nurbs.mesh -p 1 -r 3 -s 11 -dt 0.005 -tf 9
14 // ex9 -m ../../data/periodic-cube.mesh -p 0 -r 2 -s 12 -dt 0.02 -tf 8 -o 2
15 //
16 // Description: This example code solves the time-dependent advection equation
17 // du/dt + v.grad(u) = 0, where v is a given fluid velocity, and
18 // u0(x)=u(0,x) is a given initial condition.
19 //
20 // The example demonstrates the use of Discontinuous Galerkin (DG)
21 // bilinear forms in MFEM (face integrators), the use of explicit
22 // ODE time integrators, the definition of periodic boundary
23 // conditions through periodic meshes, as well as the use of GLVis
24 // for persistent visualization of a time-evolving solution. The
25 // saving of time-dependent data files for external visualization
26 // with VisIt (visit.llnl.gov) is also illustrated.
27 
28 #include "mfem.hpp"
29 #include <fstream>
30 #include <iostream>
31 #include <algorithm>
32 
33 #ifndef MFEM_USE_SUNDIALS
34 #error This example requires that MFEM is built with MFEM_USE_SUNDIALS=YES
35 #endif
36 
37 using namespace std;
38 using namespace mfem;
39 
40 // Choice for the problem setup. The fluid velocity, initial condition and
41 // inflow boundary condition are chosen based on this parameter.
42 int problem;
43 
44 // Velocity coefficient
45 void velocity_function(const Vector &x, Vector &v);
46 
47 // Initial condition
48 double u0_function(const Vector &x);
49 
50 // Inflow boundary condition
51 double inflow_function(const Vector &x);
52 
53 // Mesh bounding box
55 
56 
57 /** A time-dependent operator for the right-hand side of the ODE. The DG weak
58  form of du/dt = -v.grad(u) is M du/dt = K u + b, where M and K are the mass
59  and advection matrices, and b describes the flow on the boundary. This can
60  be written as a general ODE, du/dt = M^{-1} (K u + b), and this class is
61  used to evaluate the right-hand side. */
62 class FE_Evolution : public TimeDependentOperator
63 {
64 private:
65  SparseMatrix &M, &K;
66  const Vector &b;
67  DSmoother M_prec;
68  CGSolver M_solver;
69 
70  mutable Vector z;
71 
72 public:
73  FE_Evolution(SparseMatrix &_M, SparseMatrix &_K, const Vector &_b);
74 
75  virtual void Mult(const Vector &x, Vector &y) const;
76 
77  virtual ~FE_Evolution() { }
78 };
79 
80 
81 int main(int argc, char *argv[])
82 {
83  // 1. Parse command-line options.
84  problem = 0;
85  const char *mesh_file = "../../data/periodic-hexagon.mesh";
86  int ref_levels = 2;
87  int order = 3;
88  int ode_solver_type = 4;
89  double t_final = 10.0;
90  double dt = 0.01;
91  bool visualization = true;
92  bool visit = false;
93  bool binary = false;
94  int vis_steps = 5;
95 
96  // Relative and absolute tolerances for CVODE and ARKODE.
97  const double reltol = 1e-2, abstol = 1e-2;
98 
99  int precision = 8;
100  cout.precision(precision);
101 
102  OptionsParser args(argc, argv);
103  args.AddOption(&mesh_file, "-m", "--mesh",
104  "Mesh file to use.");
105  args.AddOption(&problem, "-p", "--problem",
106  "Problem setup to use. See options in velocity_function().");
107  args.AddOption(&ref_levels, "-r", "--refine",
108  "Number of times to refine the mesh uniformly.");
109  args.AddOption(&order, "-o", "--order",
110  "Order (degree) of the finite elements.");
111  args.AddOption(&ode_solver_type, "-s", "--ode-solver",
112  "ODE solver: 1 - Forward Euler,\n\t"
113  " 2 - RK2 SSP, 3 - RK3 SSP, 4 - RK4, 6 - RK6,\n\t"
114  " 11 - CVODE (adaptive order) explicit,\n\t"
115  " 12 - ARKODE default (4th order) explicit,\n\t"
116  " 13 - ARKODE RK8.");
117  args.AddOption(&t_final, "-tf", "--t-final",
118  "Final time; start time is 0.");
119  args.AddOption(&dt, "-dt", "--time-step",
120  "Time step.");
121  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
122  "--no-visualization",
123  "Enable or disable GLVis visualization.");
124  args.AddOption(&visit, "-visit", "--visit-datafiles", "-no-visit",
125  "--no-visit-datafiles",
126  "Save data files for VisIt (visit.llnl.gov) visualization.");
127  args.AddOption(&binary, "-binary", "--binary-datafiles", "-ascii",
128  "--ascii-datafiles",
129  "Use binary (Sidre) or ascii format for VisIt data files.");
130  args.AddOption(&vis_steps, "-vs", "--visualization-steps",
131  "Visualize every n-th timestep.");
132  args.Parse();
133  if (!args.Good())
134  {
135  args.PrintUsage(cout);
136  return 1;
137  }
138  args.PrintOptions(cout);
139 
140  // 2. Read the mesh from the given mesh file. We can handle geometrically
141  // periodic meshes in this code.
142  Mesh *mesh = new Mesh(mesh_file, 1, 1);
143  int dim = mesh->Dimension();
144 
145  // 3. Define the ODE solver used for time integration. Several explicit
146  // Runge-Kutta methods are available.
147  ODESolver *ode_solver = NULL;
148  CVODESolver *cvode = NULL;
149  ARKODESolver *arkode = NULL;
150  switch (ode_solver_type)
151  {
152  case 1: ode_solver = new ForwardEulerSolver; break;
153  case 2: ode_solver = new RK2Solver(1.0); break;
154  case 3: ode_solver = new RK3SSPSolver; break;
155  case 4: ode_solver = new RK4Solver; break;
156  case 6: ode_solver = new RK6Solver; break;
157  case 11:
158  cvode = new CVODESolver(CV_ADAMS, CV_FUNCTIONAL);
159  cvode->SetSStolerances(reltol, abstol);
160  cvode->SetMaxStep(dt);
161  ode_solver = cvode; break;
162  case 12:
163  case 13:
164  arkode = new ARKODESolver(ARKODESolver::EXPLICIT);
165  arkode->SetSStolerances(reltol, abstol);
166  arkode->SetMaxStep(dt);
167  if (ode_solver_type == 13) { arkode->SetERKTableNum(FEHLBERG_13_7_8); }
168  ode_solver = arkode; break;
169  default:
170  cout << "Unknown ODE solver type: " << ode_solver_type << '\n';
171  return 3;
172  }
173 
174  // 4. Refine the mesh to increase the resolution. In this example we do
175  // 'ref_levels' of uniform refinement, where 'ref_levels' is a
176  // command-line parameter. If the mesh is of NURBS type, we convert it to
177  // a (piecewise-polynomial) high-order mesh.
178  for (int lev = 0; lev < ref_levels; lev++)
179  {
180  mesh->UniformRefinement();
181  }
182  if (mesh->NURBSext)
183  {
184  mesh->SetCurvature(max(order, 1));
185  }
186  mesh->GetBoundingBox(bb_min, bb_max, max(order, 1));
187 
188  // 5. Define the discontinuous DG finite element space of the given
189  // polynomial order on the refined mesh.
190  DG_FECollection fec(order, dim);
191  FiniteElementSpace fes(mesh, &fec);
192 
193  cout << "Number of unknowns: " << fes.GetVSize() << endl;
194 
195  // 6. Set up and assemble the bilinear and linear forms corresponding to the
196  // DG discretization. The DGTraceIntegrator involves integrals over mesh
197  // interior faces.
201 
202  BilinearForm m(&fes);
204  BilinearForm k(&fes);
205  k.AddDomainIntegrator(new ConvectionIntegrator(velocity, -1.0));
207  new TransposeIntegrator(new DGTraceIntegrator(velocity, 1.0, -0.5)));
209  new TransposeIntegrator(new DGTraceIntegrator(velocity, 1.0, -0.5)));
210 
211  LinearForm b(&fes);
213  new BoundaryFlowIntegrator(inflow, velocity, -1.0, -0.5));
214 
215  m.Assemble();
216  m.Finalize();
217  int skip_zeros = 0;
218  k.Assemble(skip_zeros);
219  k.Finalize(skip_zeros);
220  b.Assemble();
221 
222  // 7. Define the initial conditions, save the corresponding grid function to
223  // a file and (optionally) save data in the VisIt format and initialize
224  // GLVis visualization.
225  GridFunction u(&fes);
226  u.ProjectCoefficient(u0);
227 
228  {
229  ofstream omesh("ex9.mesh");
230  omesh.precision(precision);
231  mesh->Print(omesh);
232  ofstream osol("ex9-init.gf");
233  osol.precision(precision);
234  u.Save(osol);
235  }
236 
237  // Create data collection for solution output: either VisItDataCollection for
238  // ascii data files, or SidreDataCollection for binary data files.
239  DataCollection *dc = NULL;
240  if (visit)
241  {
242  if (binary)
243  {
244 #ifdef MFEM_USE_SIDRE
245  dc = new SidreDataCollection("Example9", mesh);
246 #else
247  MFEM_ABORT("Must build with MFEM_USE_SIDRE=YES for binary output.");
248 #endif
249  }
250  else
251  {
252  dc = new VisItDataCollection("Example9", mesh);
253  dc->SetPrecision(precision);
254  }
255  dc->RegisterField("solution", &u);
256  dc->SetCycle(0);
257  dc->SetTime(0.0);
258  dc->Save();
259  }
260 
261  socketstream sout;
262  if (visualization)
263  {
264  char vishost[] = "localhost";
265  int visport = 19916;
266  sout.open(vishost, visport);
267  if (!sout)
268  {
269  cout << "Unable to connect to GLVis server at "
270  << vishost << ':' << visport << endl;
271  visualization = false;
272  cout << "GLVis visualization disabled.\n";
273  }
274  else
275  {
276  sout.precision(precision);
277  sout << "solution\n" << *mesh << u;
278  sout << "pause\n";
279  sout << flush;
280  cout << "GLVis visualization paused."
281  << " Press space (in the GLVis window) to resume it.\n";
282  }
283  }
284 
285  // 8. Define the time-dependent evolution operator describing the ODE
286  // right-hand side, and perform time-integration (looping over the time
287  // iterations, ti, with a time-step dt).
288  FE_Evolution adv(m.SpMat(), k.SpMat(), b);
289 
290  double t = 0.0;
291  adv.SetTime(t);
292  ode_solver->Init(adv);
293 
294  bool done = false;
295  for (int ti = 0; !done; )
296  {
297  double dt_real = min(dt, t_final - t);
298  ode_solver->Step(u, t, dt_real);
299  ti++;
300 
301  done = (t >= t_final - 1e-8*dt);
302 
303  if (done || ti % vis_steps == 0)
304  {
305  cout << "time step: " << ti << ", time: " << t << endl;
306  if (cvode) { cvode->PrintInfo(); }
307  if (arkode) { arkode->PrintInfo(); }
308 
309  if (visualization)
310  {
311  sout << "solution\n" << *mesh << u << flush;
312  }
313 
314  if (visit)
315  {
316  dc->SetCycle(ti);
317  dc->SetTime(t);
318  dc->Save();
319  }
320  }
321  }
322 
323  // 9. Save the final solution. This output can be viewed later using GLVis:
324  // "glvis -m ex9.mesh -g ex9-final.gf".
325  {
326  ofstream osol("ex9-final.gf");
327  osol.precision(precision);
328  u.Save(osol);
329  }
330 
331  // 10. Free the used memory.
332  delete ode_solver;
333  delete dc;
334 
335  return 0;
336 }
337 
338 
339 // Implementation of class FE_Evolution
340 FE_Evolution::FE_Evolution(SparseMatrix &_M, SparseMatrix &_K, const Vector &_b)
341  : TimeDependentOperator(_M.Size()), M(_M), K(_K), b(_b), z(_M.Size())
342 {
343  M_solver.SetPreconditioner(M_prec);
344  M_solver.SetOperator(M);
345 
346  M_solver.iterative_mode = false;
347  M_solver.SetRelTol(1e-9);
348  M_solver.SetAbsTol(0.0);
349  M_solver.SetMaxIter(100);
350  M_solver.SetPrintLevel(0);
351 }
352 
353 void FE_Evolution::Mult(const Vector &x, Vector &y) const
354 {
355  // y = M^{-1} (K x + b)
356  K.Mult(x, z);
357  z += b;
358  M_solver.Mult(z, y);
359 }
360 
361 
362 // Velocity coefficient
363 void velocity_function(const Vector &x, Vector &v)
364 {
365  int dim = x.Size();
366 
367  // map to the reference [-1,1] domain
368  Vector X(dim);
369  for (int i = 0; i < dim; i++)
370  {
371  double center = (bb_min[i] + bb_max[i]) * 0.5;
372  X(i) = 2 * (x(i) - center) / (bb_max[i] - bb_min[i]);
373  }
374 
375  switch (problem)
376  {
377  case 0:
378  {
379  // Translations in 1D, 2D, and 3D
380  switch (dim)
381  {
382  case 1: v(0) = 1.0; break;
383  case 2: v(0) = sqrt(2./3.); v(1) = sqrt(1./3.); break;
384  case 3: v(0) = sqrt(3./6.); v(1) = sqrt(2./6.); v(2) = sqrt(1./6.);
385  break;
386  }
387  break;
388  }
389  case 1:
390  case 2:
391  {
392  // Clockwise rotation in 2D around the origin
393  const double w = M_PI/2;
394  switch (dim)
395  {
396  case 1: v(0) = 1.0; break;
397  case 2: v(0) = w*X(1); v(1) = -w*X(0); break;
398  case 3: v(0) = w*X(1); v(1) = -w*X(0); v(2) = 0.0; break;
399  }
400  break;
401  }
402  case 3:
403  {
404  // Clockwise twisting rotation in 2D around the origin
405  const double w = M_PI/2;
406  double d = max((X(0)+1.)*(1.-X(0)),0.) * max((X(1)+1.)*(1.-X(1)),0.);
407  d = d*d;
408  switch (dim)
409  {
410  case 1: v(0) = 1.0; break;
411  case 2: v(0) = d*w*X(1); v(1) = -d*w*X(0); break;
412  case 3: v(0) = d*w*X(1); v(1) = -d*w*X(0); v(2) = 0.0; break;
413  }
414  break;
415  }
416  }
417 }
418 
419 // Initial condition
420 double u0_function(const Vector &x)
421 {
422  int dim = x.Size();
423 
424  // map to the reference [-1,1] domain
425  Vector X(dim);
426  for (int i = 0; i < dim; i++)
427  {
428  double center = (bb_min[i] + bb_max[i]) * 0.5;
429  X(i) = 2 * (x(i) - center) / (bb_max[i] - bb_min[i]);
430  }
431 
432  switch (problem)
433  {
434  case 0:
435  case 1:
436  {
437  switch (dim)
438  {
439  case 1:
440  return exp(-40.*pow(X(0)-0.5,2));
441  case 2:
442  case 3:
443  {
444  double rx = 0.45, ry = 0.25, cx = 0., cy = -0.2, w = 10.;
445  if (dim == 3)
446  {
447  const double s = (1. + 0.25*cos(2*M_PI*X(2)));
448  rx *= s;
449  ry *= s;
450  }
451  return ( erfc(w*(X(0)-cx-rx))*erfc(-w*(X(0)-cx+rx)) *
452  erfc(w*(X(1)-cy-ry))*erfc(-w*(X(1)-cy+ry)) )/16;
453  }
454  }
455  }
456  case 2:
457  {
458  double x_ = X(0), y_ = X(1), rho, phi;
459  rho = hypot(x_, y_);
460  phi = atan2(y_, x_);
461  return pow(sin(M_PI*rho),2)*sin(3*phi);
462  }
463  case 3:
464  {
465  const double f = M_PI;
466  return sin(f*X(0))*sin(f*X(1));
467  }
468  }
469  return 0.0;
470 }
471 
472 // Inflow boundary condition (zero for the problems considered in this example)
473 double inflow_function(const Vector &x)
474 {
475  switch (problem)
476  {
477  case 0:
478  case 1:
479  case 2:
480  case 3: return 0.0;
481  }
482  return 0.0;
483 }
void SetPrecision(int prec)
Set the precision (number of digits) used for the text output of doubles.
int GetVSize() const
Definition: fespace.hpp:163
Conjugate gradient method.
Definition: solvers.hpp:111
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:27
void SetCycle(int c)
Set time cycle (for time-dependent simulations)
Data type for scaled Jacobi-type smoother of sparse matrix.
void SetSStolerances(double reltol, double abstol)
Set the scalar relative and scalar absolute tolerances.
Definition: sundials.cpp:189
void Assemble(int skip_zeros=1)
Assembles the form i.e. sums over all domain/bdr integrators.
int problem
void velocity_function(const Vector &x, Vector &v)
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:140
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:468
void GetBoundingBox(Vector &min, Vector &max, int ref=2)
Definition: mesh.cpp:88
void Assemble()
Assembles the linear form i.e. sums over all domain/bdr integrators.
Definition: linearform.cpp:42
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]...
int Size() const
Returns the size of the vector.
Definition: vector.hpp:106
Vector bb_min
Abstract class for solving systems of ODEs: dx/dt = f(x,t)
Definition: ode.hpp:22
int main(int argc, char *argv[])
STL namespace.
void SetSStolerances(double reltol, double abstol)
Specify the scalar relative and scalar absolute tolerances.
Definition: sundials.cpp:432
void SetERKTableNum(int table_num)
Choose a specific Butcher table for explicit RK method.
Definition: sundials.cpp:481
Data collection with Sidre routines following the Conduit mesh blueprint specification.
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
Definition: gridfunc.cpp:2185
int dim
Definition: ex3.cpp:47
Data type sparse matrix.
Definition: sparsemat.hpp:38
Wrapper for SUNDIALS' CVODE library – Multi-step time integration.
Definition: sundials.hpp:133
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:6684
Data collection with VisIt I/O routines.
void SetCurvature(int order, bool discont=false, int space_dim=-1, int ordering=1)
Definition: mesh.cpp:3278
double inflow_function(const Vector &x)
void SetMaxStep(double dt_max)
Set the maximum time step of the linear multistep method.
Definition: sundials.hpp:183
void AddBdrFaceIntegrator(LinearFormIntegrator *lfi)
Adds new Boundary Face Integrator.
Definition: linearform.cpp:29
int Dimension() const
Definition: mesh.hpp:611
void PrintUsage(std::ostream &out) const
Definition: optparser.cpp:434
void SetTime(double t)
Set physical time (for time-dependent simulations)
Vector bb_max
Wrapper for SUNDIALS' ARKODE library – Runge-Kutta time integration.
Definition: sundials.hpp:220
The classical explicit forth-order Runge-Kutta method, RK4.
Definition: ode.hpp:147
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
NURBSExtension * NURBSext
Optional NURBS mesh extension.
Definition: mesh.hpp:144
virtual void Finalize(int skip_zeros=1)
Finalizes the matrix initialization.
void AddInteriorFaceIntegrator(BilinearFormIntegrator *bfi)
Adds new interior Face Integrator.
void AddDomainIntegrator(BilinearFormIntegrator *bfi)
Adds new Domain Integrator.
void PrintOptions(std::ostream &out) const
Definition: optparser.cpp:304
void ProjectCoefficient(Coefficient &coeff)
Definition: gridfunc.cpp:1232
int open(const char hostname[], int port)
class for C-function coefficient
Vector data type.
Definition: vector.hpp:36
Class for linear form - Vector with associated FE space and LFIntegrators.
Definition: linearform.hpp:23
void PrintInfo() const
Print CVODE statistics.
Definition: sundials.cpp:355
virtual void RegisterField(const std::string &field_name, GridFunction *gf)
Add a grid function to the collection.
const SparseMatrix & SpMat() const
Returns a reference to the sparse matrix.
double u0_function(const Vector &x)
The classical forward Euler method.
Definition: ode.hpp:101
void SetMaxStep(double dt_max)
Set the maximum time step of the Runge-Kutta method.
Definition: sundials.hpp:276
void PrintInfo() const
Print ARKODE statistics.
Definition: sundials.cpp:636
void AddBdrFaceIntegrator(BilinearFormIntegrator *bfi)
Adds new boundary Face Integrator.
Arbitrary order "L2-conforming" discontinuous finite elements.
Definition: fe_coll.hpp:195
virtual void Print(std::ostream &out=std::cout) const
Definition: mesh.hpp:988
bool Good() const
Definition: optparser.hpp:120
alpha (q . grad u, v)