Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
time_dependent.h
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>
26 # include <deal.II/base/smartpointer.h>
27 # include <deal.II/base/subscriptor.h>
28 
29 # include <utility>
30 # include <vector>
31 
32 DEAL_II_NAMESPACE_OPEN
33 
34 // forward declarations
35 class TimeStepBase;
36 template <typename number>
37 class Vector;
38 template <int dim, int spacedim>
39 class Triangulation;
40 
363 {
364 public:
372  {
377  TimeSteppingData(const unsigned int look_ahead,
378  const unsigned int look_back);
379 
398  const unsigned int look_ahead;
399 
412  const unsigned int look_back;
413  };
414 
420  {
429  };
430 
434  TimeDependent(const TimeSteppingData &data_primal,
435  const TimeSteppingData &data_dual,
436  const TimeSteppingData &data_postprocess);
437 
438 
444  virtual ~TimeDependent();
445 
466  void
467  insert_timestep(const TimeStepBase *position, TimeStepBase *new_timestep);
468 
478  void
479  add_timestep(TimeStepBase *new_timestep);
480 
493  void
494  delete_timestep(const unsigned int position);
495 
504  void
506 
515  void
517 
526  void
527  postprocess();
528 
555  template <typename InitFunctionObject, typename LoopFunctionObject>
556  void
557  do_loop(InitFunctionObject init_function,
558  LoopFunctionObject loop_function,
559  const TimeSteppingData &timestepping_data,
560  const Direction direction);
561 
562 
580  virtual void
581  start_sweep(const unsigned int sweep_no);
582 
596  virtual void
597  end_sweep();
598 
603  std::size_t
604  memory_consumption() const;
605 
610  "You cannot insert a time step at the specified position.");
611 
612 protected:
618  std::vector<SmartPointer<TimeStepBase, TimeDependent>> timesteps;
619 
624  unsigned int sweep_no;
625 
631 
637 
643 
644 private:
649  void
650  end_sweep(const unsigned int begin_timestep, const unsigned int end_timestep);
651 };
652 
653 
654 
664 class TimeStepBase : public Subscriptor
665 {
666 public:
671  {
675  primal_problem = 0x0,
679  dual_problem = 0x1,
684  };
685 
689  TimeStepBase(const double time);
690 
694  virtual ~TimeStepBase() override = default;
695 
700  TimeStepBase(const TimeStepBase &) = delete;
701 
706  TimeStepBase &
707  operator=(const TimeStepBase &) = delete;
708 
724  virtual void
725  wake_up(const unsigned int);
726 
736  virtual void
737  sleep(const unsigned int);
738 
754  virtual void
755  start_sweep();
756 
762  virtual void
763  end_sweep();
764 
772  virtual void
773  init_for_primal_problem();
774 
778  virtual void
779  init_for_dual_problem();
780 
784  virtual void
785  init_for_postprocessing();
786 
794  virtual void
795  solve_primal_problem() = 0;
796 
806  virtual void
808 
818  virtual void
819  postprocess_timestep();
820 
824  double
825  get_time() const;
826 
831  unsigned int
832  get_timestep_no() const;
833 
847  double
848  get_backward_timestep() const;
849 
855  double
856  get_forward_timestep() const;
857 
866  virtual std::size_t
867  memory_consumption() const;
868 
869 protected:
874 
879 
884  unsigned int sweep_no;
885 
891  unsigned int timestep_no;
892 
896  const double time;
897 
903  unsigned int next_action;
904 
905 private:
913  void
914  set_previous_timestep(const TimeStepBase *previous);
915 
923  void
924  set_next_timestep(const TimeStepBase *next);
925 
932  void
933  set_timestep_no(const unsigned int step_no);
934 
939  void
940  set_sweep_no(const unsigned int sweep_no);
941 
942  // make the manager object a friend
943  friend class TimeDependent;
944 };
945 
946 
947 
957 {
965  template <int dim>
966  struct Flags
967  {
971  Flags();
972 
977  Flags(const bool delete_and_rebuild_tria,
978  const unsigned int wakeup_level_to_build_grid,
979  const unsigned int sleep_level_to_delete_grid);
980 
992 
1001  const unsigned int wakeup_level_to_build_grid;
1002 
1007  const unsigned int sleep_level_to_delete_grid;
1008  };
1009 
1010 
1011 
1131  template <int dim>
1133  {
1139  using CorrectionRelaxations =
1140  std::vector<std::vector<std::pair<unsigned int, double>>>;
1141 
1146 
1151  RefinementFlags(const unsigned int max_refinement_level = 0,
1152  const unsigned int first_sweep_with_correction = 0,
1153  const unsigned int min_cells_for_correction = 0,
1154  const double cell_number_corridor_top = (1 << dim),
1155  const double cell_number_corridor_bottom = 1,
1156  const CorrectionRelaxations &correction_relaxations =
1158  const unsigned int cell_number_correction_steps = 0,
1159  const bool mirror_flags_to_previous_grid = false,
1160  const bool adapt_grids = false);
1161 
1170  const unsigned int max_refinement_level;
1171 
1177  const unsigned int first_sweep_with_correction;
1178 
1179 
1184  const unsigned int min_cells_for_correction;
1185 
1192 
1197 
1201  const std::vector<std::vector<std::pair<unsigned int, double>>>
1203 
1209  const unsigned int cell_number_correction_steps;
1210 
1228 
1232  const bool adapt_grids;
1233 
1237  DeclException1(ExcInvalidValue,
1238  double,
1239  << "The value " << arg1
1240  << " for the cell number corridor does not fulfill "
1241  "its natural requirements.");
1242  };
1243 
1244 
1245 
1252  template <int dim>
1254  {
1258  RefinementData(const double refinement_threshold,
1259  const double coarsening_threshold = 0);
1260 
1267  const double refinement_threshold;
1268 
1273  const double coarsening_threshold;
1274 
1278  DeclException1(ExcInvalidValue,
1279  double,
1280  << "The value " << arg1
1281  << " for the cell refinement thresholds does not fulfill "
1282  "its natural requirements.");
1283  };
1284 } // namespace TimeStepBase_Tria_Flags
1285 
1286 
1287 
1307 template <int dim>
1309 {
1310 public:
1316  using RefinementFlags =
1318  using RefinementData = typename TimeStepBase_Tria_Flags::RefinementData<dim>;
1319 
1320 
1326  {
1330  grid_refinement = 0x1000
1331  };
1332 
1333 
1343 
1357  const double time,
1358  const Triangulation<dim, dim> &coarse_grid,
1359  const Flags & flags,
1360  const RefinementFlags & refinement_flags = RefinementFlags());
1361 
1366  virtual ~TimeStepBase_Tria() override;
1367 
1388  virtual void
1389  wake_up(const unsigned int wakeup_level) override;
1390 
1404  virtual void
1405  sleep(const unsigned int) override;
1406 
1421  void
1422  refine_grid(const RefinementData data);
1423 
1429  virtual void
1430  init_for_refinement();
1431 
1439  virtual void
1440  get_tria_refinement_criteria(Vector<float> &criteria) const = 0;
1441 
1446  void
1447  save_refine_flags();
1448 
1457  virtual std::size_t
1458  memory_consumption() const override;
1459 
1463  DeclExceptionMsg(ExcGridNotDeleted,
1464  "When calling restore_grid(), you must have previously "
1465  "deleted the triangulation.");
1466 
1467 protected:
1476 
1484 
1489  const Flags flags;
1490 
1495  const RefinementFlags refinement_flags;
1496 
1497 private:
1503  std::vector<std::vector<bool>> refine_flags;
1504 
1508  std::vector<std::vector<bool>> coarsen_flags;
1509 
1514  void
1515  restore_grid();
1516 };
1517 
1518 
1519 
1520 /*--------------------------- template functions ----------------------------*/
1521 
1522 template <typename InitFunctionObject, typename LoopFunctionObject>
1523 void
1524 TimeDependent::do_loop(InitFunctionObject init_function,
1525  LoopFunctionObject loop_function,
1526  const TimeSteppingData &timestepping_data,
1527  const Direction direction)
1528 {
1529  // the following functions looks quite
1530  // disrupted due to the recurring switches
1531  // for forward and backward running loops.
1532  //
1533  // I chose to switch at every place where
1534  // it is needed, since it is so easy
1535  // to overlook something when you change
1536  // some code at one place when it needs
1537  // to be changed at a second place, here
1538  // for the other direction, also.
1539 
1540  const unsigned int n_timesteps = timesteps.size();
1541 
1542  // initialize the time steps for
1543  // a round of this loop
1544  for (unsigned int step = 0; step < n_timesteps; ++step)
1545  switch (direction)
1546  {
1547  case forward:
1548  init_function((&*timesteps[step]));
1549  break;
1550  case backward:
1551  init_function((&*timesteps[n_timesteps - step - 1]));
1552  break;
1553  };
1554 
1555 
1556  // wake up the first few time levels
1557  for (int step = -static_cast<int>(timestepping_data.look_ahead); step < 0;
1558  ++step)
1559  for (int look_ahead = 0;
1560  look_ahead <= static_cast<int>(timestepping_data.look_ahead);
1561  ++look_ahead)
1562  switch (direction)
1563  {
1564  case forward:
1565  if (step + look_ahead >= 0)
1566  timesteps[step + look_ahead]->wake_up(look_ahead);
1567  break;
1568  case backward:
1569  if (n_timesteps - (step + look_ahead) < n_timesteps)
1570  timesteps[n_timesteps - (step + look_ahead)]->wake_up(look_ahead);
1571  break;
1572  };
1573 
1574 
1575  for (unsigned int step = 0; step < n_timesteps; ++step)
1576  {
1577  // first thing: wake up the
1578  // timesteps ahead as necessary
1579  for (unsigned int look_ahead = 0;
1580  look_ahead <= timestepping_data.look_ahead;
1581  ++look_ahead)
1582  switch (direction)
1583  {
1584  case forward:
1585  if (step + look_ahead < n_timesteps)
1586  timesteps[step + look_ahead]->wake_up(look_ahead);
1587  break;
1588  case backward:
1589  if (n_timesteps > (step + look_ahead))
1590  timesteps[n_timesteps - (step + look_ahead) - 1]->wake_up(
1591  look_ahead);
1592  break;
1593  };
1594 
1595 
1596  // actually do the work
1597  switch (direction)
1598  {
1599  case forward:
1600  loop_function((&*timesteps[step]));
1601  break;
1602  case backward:
1603  loop_function((&*timesteps[n_timesteps - step - 1]));
1604  break;
1605  };
1606 
1607  // let the timesteps behind sleep
1608  for (unsigned int look_back = 0; look_back <= timestepping_data.look_back;
1609  ++look_back)
1610  switch (direction)
1611  {
1612  case forward:
1613  if (step >= look_back)
1614  timesteps[step - look_back]->sleep(look_back);
1615  break;
1616  case backward:
1617  if (n_timesteps - (step - look_back) <= n_timesteps)
1618  timesteps[n_timesteps - (step - look_back) - 1]->sleep(
1619  look_back);
1620  break;
1621  }
1622  }
1623 
1624  // make the last few timesteps sleep
1625  for (int step = n_timesteps;
1626  step < static_cast<int>(n_timesteps + timestepping_data.look_back);
1627  ++step)
1628  for (int look_back = 0;
1629  look_back <= static_cast<int>(timestepping_data.look_back);
1630  ++look_back)
1631  switch (direction)
1632  {
1633  case forward:
1634  if ((step - look_back >= 0) &&
1635  (step - look_back < static_cast<int>(n_timesteps)))
1636  timesteps[step - look_back]->sleep(look_back);
1637  break;
1638  case backward:
1639  if ((step - look_back >= 0) &&
1640  (step - look_back < static_cast<int>(n_timesteps)))
1641  timesteps[n_timesteps - (step - look_back) - 1]->sleep(look_back);
1642  break;
1643  };
1644 }
1645 
1646 DEAL_II_NAMESPACE_CLOSE
1647 
1648 /*---------------------------- time-dependent.h ---------------------------*/
1649 #endif
1650 /*---------------------------- 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
LinearAlgebra::distributed::Vector< Number > Vector
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)
static CorrectionRelaxations default_correction_relaxations
const unsigned int sleep_level_to_delete_grid
void delete_timestep(const unsigned int position)
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
SmartPointer< Triangulation< dim, dim >, TimeStepBase_Tria< dim > > tria
std::vector< std::vector< bool > > coarsen_flags
virtual void end_sweep()
unsigned int sweep_no
TimeSteppingData(const unsigned int look_ahead, const unsigned int look_back)