Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
time_dependent.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2019 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_time_dependent_h
17 # define dealii_time_dependent_h
18 
19 
20 /*---------------------------- time-dependent.h ---------------------------*/
21 
22 
23 # include <deal.II/base/config.h>
24 
25 # include <deal.II/base/exceptions.h>
28 
29 # include <utility>
30 # include <vector>
31 
33 
34 // forward declarations
35 # ifndef DOXYGEN
36 class TimeStepBase;
37 template <typename number>
38 class Vector;
39 template <int dim, int spacedim>
40 class Triangulation;
41 # endif
42 
365 {
366 public:
374  {
379  TimeSteppingData(const unsigned int look_ahead,
380  const unsigned int look_back);
381 
400  const unsigned int look_ahead;
401 
414  const unsigned int look_back;
415  };
416 
422  {
431  };
432 
436  TimeDependent(const TimeSteppingData &data_primal,
437  const TimeSteppingData &data_dual,
438  const TimeSteppingData &data_postprocess);
439 
440 
446  virtual ~TimeDependent();
447 
468  void
469  insert_timestep(const TimeStepBase *position, TimeStepBase *new_timestep);
470 
480  void
481  add_timestep(TimeStepBase *new_timestep);
482 
495  void
496  delete_timestep(const unsigned int position);
497 
506  void
508 
517  void
519 
528  void
529  postprocess();
530 
561  template <typename InitFunctionObject, typename LoopFunctionObject>
562  void
563  do_loop(InitFunctionObject init_function,
564  LoopFunctionObject loop_function,
565  const TimeSteppingData &timestepping_data,
566  const Direction direction);
567 
568 
586  virtual void
587  start_sweep(const unsigned int sweep_no);
588 
602  virtual void
603  end_sweep();
604 
609  std::size_t
610  memory_consumption() const;
611 
616  "You cannot insert a time step at the specified position.");
617 
618 protected:
624  std::vector<SmartPointer<TimeStepBase, TimeDependent>> timesteps;
625 
630  unsigned int sweep_no;
631 
637 
643 
649 
650 private:
655  void
656  end_sweep(const unsigned int begin_timestep, const unsigned int end_timestep);
657 };
658 
659 
660 
670 class TimeStepBase : public Subscriptor
671 {
672 public:
677  {
681  primal_problem = 0x0,
685  dual_problem = 0x1,
690  };
691 
695  TimeStepBase(const double time);
696 
700  virtual ~TimeStepBase() override = default;
701 
706  TimeStepBase(const TimeStepBase &) = delete;
707 
712  TimeStepBase &
713  operator=(const TimeStepBase &) = delete;
714 
730  virtual void
731  wake_up(const unsigned int);
732 
742  virtual void
743  sleep(const unsigned int);
744 
760  virtual void
761  start_sweep();
762 
768  virtual void
769  end_sweep();
770 
778  virtual void
779  init_for_primal_problem();
780 
784  virtual void
785  init_for_dual_problem();
786 
790  virtual void
791  init_for_postprocessing();
792 
800  virtual void
801  solve_primal_problem() = 0;
802 
812  virtual void
814 
824  virtual void
825  postprocess_timestep();
826 
830  double
831  get_time() const;
832 
837  unsigned int
838  get_timestep_no() const;
839 
853  double
854  get_backward_timestep() const;
855 
861  double
862  get_forward_timestep() const;
863 
872  virtual std::size_t
873  memory_consumption() const;
874 
875 protected:
880 
885 
890  unsigned int sweep_no;
891 
897  unsigned int timestep_no;
898 
902  const double time;
903 
909  unsigned int next_action;
910 
911 private:
919  void
920  set_previous_timestep(const TimeStepBase *previous);
921 
929  void
930  set_next_timestep(const TimeStepBase *next);
931 
938  void
939  set_timestep_no(const unsigned int step_no);
940 
945  void
946  set_sweep_no(const unsigned int sweep_no);
947 
948  // make the manager object a friend
949  friend class TimeDependent;
950 };
951 
952 
953 
963 {
971  template <int dim>
972  struct Flags
973  {
977  Flags();
978 
983  Flags(const bool delete_and_rebuild_tria,
984  const unsigned int wakeup_level_to_build_grid,
985  const unsigned int sleep_level_to_delete_grid);
986 
998 
1007  const unsigned int wakeup_level_to_build_grid;
1008 
1013  const unsigned int sleep_level_to_delete_grid;
1014  };
1015 
1016 
1017 
1137  template <int dim>
1139  {
1145  using CorrectionRelaxations =
1146  std::vector<std::vector<std::pair<unsigned int, double>>>;
1147 
1152 
1157  RefinementFlags(const unsigned int max_refinement_level = 0,
1158  const unsigned int first_sweep_with_correction = 0,
1159  const unsigned int min_cells_for_correction = 0,
1160  const double cell_number_corridor_top = (1 << dim),
1161  const double cell_number_corridor_bottom = 1,
1162  const CorrectionRelaxations &correction_relaxations =
1164  const unsigned int cell_number_correction_steps = 0,
1165  const bool mirror_flags_to_previous_grid = false,
1166  const bool adapt_grids = false);
1167 
1176  const unsigned int max_refinement_level;
1177 
1183  const unsigned int first_sweep_with_correction;
1184 
1185 
1190  const unsigned int min_cells_for_correction;
1191 
1198 
1203 
1207  const std::vector<std::vector<std::pair<unsigned int, double>>>
1209 
1215  const unsigned int cell_number_correction_steps;
1216 
1234 
1238  const bool adapt_grids;
1239 
1243  DeclException1(ExcInvalidValue,
1244  double,
1245  << "The value " << arg1
1246  << " for the cell number corridor does not fulfill "
1247  "its natural requirements.");
1248  };
1249 
1250 
1251 
1258  template <int dim>
1260  {
1264  RefinementData(const double refinement_threshold,
1265  const double coarsening_threshold = 0);
1266 
1273  const double refinement_threshold;
1274 
1279  const double coarsening_threshold;
1280 
1284  DeclException1(ExcInvalidValue,
1285  double,
1286  << "The value " << arg1
1287  << " for the cell refinement thresholds does not fulfill "
1288  "its natural requirements.");
1289  };
1290 } // namespace TimeStepBase_Tria_Flags
1291 
1292 
1293 
1313 template <int dim>
1315 {
1316 public:
1322  using RefinementFlags =
1325 
1326 
1332  {
1336  grid_refinement = 0x1000
1337  };
1338 
1339 
1349 
1363  const double time,
1364  const Triangulation<dim, dim> &coarse_grid,
1365  const Flags & flags,
1366  const RefinementFlags & refinement_flags = RefinementFlags());
1367 
1372  virtual ~TimeStepBase_Tria() override;
1373 
1394  virtual void
1395  wake_up(const unsigned int wakeup_level) override;
1396 
1410  virtual void
1411  sleep(const unsigned int) override;
1412 
1427  void
1428  refine_grid(const RefinementData data);
1429 
1435  virtual void
1436  init_for_refinement();
1437 
1445  virtual void
1446  get_tria_refinement_criteria(Vector<float> &criteria) const = 0;
1447 
1452  void
1453  save_refine_flags();
1454 
1463  virtual std::size_t
1464  memory_consumption() const override;
1465 
1469  DeclExceptionMsg(ExcGridNotDeleted,
1470  "When calling restore_grid(), you must have previously "
1471  "deleted the triangulation.");
1472 
1473 protected:
1482 
1490 
1495  const Flags flags;
1496 
1502 
1503 private:
1509  std::vector<std::vector<bool>> refine_flags;
1510 
1514  std::vector<std::vector<bool>> coarsen_flags;
1515 
1520  void
1521  restore_grid();
1522 };
1523 
1524 
1525 
1526 /*--------------------------- template functions ----------------------------*/
1527 
1528 template <typename InitFunctionObject, typename LoopFunctionObject>
1529 void
1530 TimeDependent::do_loop(InitFunctionObject init_function,
1531  LoopFunctionObject loop_function,
1532  const TimeSteppingData &timestepping_data,
1533  const Direction direction)
1534 {
1535  // the following functions looks quite
1536  // disrupted due to the recurring switches
1537  // for forward and backward running loops.
1538  //
1539  // I chose to switch at every place where
1540  // it is needed, since it is so easy
1541  // to overlook something when you change
1542  // some code at one place when it needs
1543  // to be changed at a second place, here
1544  // for the other direction, also.
1545 
1546  const unsigned int n_timesteps = timesteps.size();
1547 
1548  // initialize the time steps for
1549  // a round of this loop
1550  for (unsigned int step = 0; step < n_timesteps; ++step)
1551  switch (direction)
1552  {
1553  case forward:
1554  init_function((&*timesteps[step]));
1555  break;
1556  case backward:
1557  init_function((&*timesteps[n_timesteps - step - 1]));
1558  break;
1559  };
1560 
1561 
1562  // wake up the first few time levels
1563  for (int step = -static_cast<int>(timestepping_data.look_ahead); step < 0;
1564  ++step)
1565  for (int look_ahead = 0;
1566  look_ahead <= static_cast<int>(timestepping_data.look_ahead);
1567  ++look_ahead)
1568  switch (direction)
1569  {
1570  case forward:
1571  if (step + look_ahead >= 0)
1572  timesteps[step + look_ahead]->wake_up(look_ahead);
1573  break;
1574  case backward:
1575  if (n_timesteps - (step + look_ahead) < n_timesteps)
1576  timesteps[n_timesteps - (step + look_ahead)]->wake_up(look_ahead);
1577  break;
1578  };
1579 
1580 
1581  for (unsigned int step = 0; step < n_timesteps; ++step)
1582  {
1583  // first thing: wake up the
1584  // timesteps ahead as necessary
1585  for (unsigned int look_ahead = 0;
1586  look_ahead <= timestepping_data.look_ahead;
1587  ++look_ahead)
1588  switch (direction)
1589  {
1590  case forward:
1591  if (step + look_ahead < n_timesteps)
1592  timesteps[step + look_ahead]->wake_up(look_ahead);
1593  break;
1594  case backward:
1595  if (n_timesteps > (step + look_ahead))
1596  timesteps[n_timesteps - (step + look_ahead) - 1]->wake_up(
1597  look_ahead);
1598  break;
1599  };
1600 
1601 
1602  // actually do the work
1603  switch (direction)
1604  {
1605  case forward:
1606  loop_function((&*timesteps[step]));
1607  break;
1608  case backward:
1609  loop_function((&*timesteps[n_timesteps - step - 1]));
1610  break;
1611  };
1612 
1613  // let the timesteps behind sleep
1614  for (unsigned int look_back = 0; look_back <= timestepping_data.look_back;
1615  ++look_back)
1616  switch (direction)
1617  {
1618  case forward:
1619  if (step >= look_back)
1620  timesteps[step - look_back]->sleep(look_back);
1621  break;
1622  case backward:
1623  if (n_timesteps - (step - look_back) <= n_timesteps)
1624  timesteps[n_timesteps - (step - look_back) - 1]->sleep(
1625  look_back);
1626  break;
1627  }
1628  }
1629 
1630  // make the last few timesteps sleep
1631  for (int step = n_timesteps;
1632  step < static_cast<int>(n_timesteps + timestepping_data.look_back);
1633  ++step)
1634  for (int look_back = 0;
1635  look_back <= static_cast<int>(timestepping_data.look_back);
1636  ++look_back)
1637  switch (direction)
1638  {
1639  case forward:
1640  if ((step - look_back >= 0) &&
1641  (step - look_back < static_cast<int>(n_timesteps)))
1642  timesteps[step - look_back]->sleep(look_back);
1643  break;
1644  case backward:
1645  if ((step - look_back >= 0) &&
1646  (step - look_back < static_cast<int>(n_timesteps)))
1647  timesteps[n_timesteps - (step - look_back) - 1]->sleep(look_back);
1648  break;
1649  };
1650 }
1651 
1653 
1654 /*---------------------------- time-dependent.h ---------------------------*/
1655 #endif
1656 /*---------------------------- time-dependent.h ---------------------------*/
unsigned int next_action
const std::vector< std::vector< std::pair< unsigned int, double > > > correction_relaxations
TimeDependent(const TimeSteppingData &data_primal, const TimeSteppingData &data_dual, const TimeSteppingData &data_postprocess)
void solve_primal_problem()
void add_timestep(TimeStepBase *new_timestep)
void solve_dual_problem()
const TimeStepBase * next_timestep
const TimeSteppingData timestepping_data_postprocess
const unsigned int wakeup_level_to_build_grid
const TimeStepBase * previous_timestep
const TimeSteppingData timestepping_data_dual
SmartPointer< const Triangulation< dim, dim >, TimeStepBase_Tria< dim > > coarse_grid
unsigned int timestep_no
void do_loop(InitFunctionObject init_function, LoopFunctionObject loop_function, const TimeSteppingData &timestepping_data, const Direction direction)
const RefinementFlags refinement_flags
std::vector< std::vector< std::pair< unsigned int, double > >> CorrectionRelaxations
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:518
std::size_t memory_consumption() const
const double time
std::vector< SmartPointer< TimeStepBase, TimeDependent > > timesteps
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:496
virtual ~TimeDependent()
const TimeSteppingData timestepping_data_primal
static ::ExceptionBase & ExcInvalidPosition()
virtual void start_sweep(const unsigned int sweep_no)
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
static CorrectionRelaxations default_correction_relaxations
const unsigned int sleep_level_to_delete_grid
void delete_timestep(const unsigned int position)
typename TimeStepBase_Tria_Flags::RefinementFlags< dim > RefinementFlags
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
typename TimeStepBase_Tria_Flags::Flags< dim > Flags
unsigned int sweep_no
void insert_timestep(const TimeStepBase *position, TimeStepBase *new_timestep)
std::vector< std::vector< bool > > refine_flags
typename TimeStepBase_Tria_Flags::RefinementData< dim > RefinementData
SmartPointer< Triangulation< dim, dim >, TimeStepBase_Tria< dim > > tria
std::vector< std::vector< bool > > coarsen_flags
std::string get_time()
Definition: utilities.cc:1020
virtual void end_sweep()
unsigned int sweep_no
TimeSteppingData(const unsigned int look_ahead, const unsigned int look_back)