Reference documentation for deal.II version Git 409ee4b167 2020-08-14 09:46:12 -0400
\(\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 
362 {
363 public:
371  {
376  TimeSteppingData(const unsigned int look_ahead,
377  const unsigned int look_back);
378 
397  const unsigned int look_ahead;
398 
411  const unsigned int look_back;
412  };
413 
419  {
428  };
429 
433  TimeDependent(const TimeSteppingData &data_primal,
434  const TimeSteppingData &data_dual,
435  const TimeSteppingData &data_postprocess);
436 
437 
443  virtual ~TimeDependent();
444 
465  void
466  insert_timestep(const TimeStepBase *position, TimeStepBase *new_timestep);
467 
477  void
478  add_timestep(TimeStepBase *new_timestep);
479 
492  void
493  delete_timestep(const unsigned int position);
494 
503  void
505 
514  void
516 
525  void
526  postprocess();
527 
558  template <typename InitFunctionObject, typename LoopFunctionObject>
559  void
560  do_loop(InitFunctionObject init_function,
561  LoopFunctionObject loop_function,
562  const TimeSteppingData &timestepping_data,
563  const Direction direction);
564 
565 
583  virtual void
584  start_sweep(const unsigned int sweep_no);
585 
599  virtual void
600  end_sweep();
601 
606  std::size_t
607  memory_consumption() const;
608 
613  "You cannot insert a time step at the specified position.");
614 
615 protected:
621  std::vector<SmartPointer<TimeStepBase, TimeDependent>> timesteps;
622 
627  unsigned int sweep_no;
628 
634 
640 
646 
647 private:
652  void
653  end_sweep(const unsigned int begin_timestep, const unsigned int end_timestep);
654 };
655 
656 
657 
665 class TimeStepBase : public Subscriptor
666 {
667 public:
672  {
676  primal_problem = 0x0,
680  dual_problem = 0x1,
685  };
686 
690  TimeStepBase(const double time);
691 
695  virtual ~TimeStepBase() override = default;
696 
701  TimeStepBase(const TimeStepBase &) = delete;
702 
707  TimeStepBase &
708  operator=(const TimeStepBase &) = delete;
709 
725  virtual void
726  wake_up(const unsigned int);
727 
737  virtual void
738  sleep(const unsigned int);
739 
755  virtual void
756  start_sweep();
757 
763  virtual void
764  end_sweep();
765 
773  virtual void
774  init_for_primal_problem();
775 
779  virtual void
780  init_for_dual_problem();
781 
785  virtual void
786  init_for_postprocessing();
787 
795  virtual void
796  solve_primal_problem() = 0;
797 
807  virtual void
809 
819  virtual void
820  postprocess_timestep();
821 
825  double
826  get_time() const;
827 
832  unsigned int
833  get_timestep_no() const;
834 
848  double
849  get_backward_timestep() const;
850 
856  double
857  get_forward_timestep() const;
858 
867  virtual std::size_t
868  memory_consumption() const;
869 
870 protected:
875 
880 
885  unsigned int sweep_no;
886 
892  unsigned int timestep_no;
893 
897  const double time;
898 
904  unsigned int next_action;
905 
906 private:
914  void
915  set_previous_timestep(const TimeStepBase *previous);
916 
924  void
925  set_next_timestep(const TimeStepBase *next);
926 
933  void
934  set_timestep_no(const unsigned int step_no);
935 
940  void
941  set_sweep_no(const unsigned int sweep_no);
942 
943  // make the manager object a friend
944  friend class TimeDependent;
945 };
946 
947 
948 
956 {
964  template <int dim>
965  struct Flags
966  {
970  Flags();
971 
976  Flags(const bool delete_and_rebuild_tria,
977  const unsigned int wakeup_level_to_build_grid,
978  const unsigned int sleep_level_to_delete_grid);
979 
991 
1000  const unsigned int wakeup_level_to_build_grid;
1001 
1006  const unsigned int sleep_level_to_delete_grid;
1007  };
1008 
1009 
1010 
1130  template <int dim>
1132  {
1138  using CorrectionRelaxations =
1139  std::vector<std::vector<std::pair<unsigned int, double>>>;
1140 
1145 
1150  RefinementFlags(const unsigned int max_refinement_level = 0,
1151  const unsigned int first_sweep_with_correction = 0,
1152  const unsigned int min_cells_for_correction = 0,
1153  const double cell_number_corridor_top = (1 << dim),
1154  const double cell_number_corridor_bottom = 1,
1155  const CorrectionRelaxations &correction_relaxations =
1157  const unsigned int cell_number_correction_steps = 0,
1158  const bool mirror_flags_to_previous_grid = false,
1159  const bool adapt_grids = false);
1160 
1169  const unsigned int max_refinement_level;
1170 
1176  const unsigned int first_sweep_with_correction;
1177 
1178 
1183  const unsigned int min_cells_for_correction;
1184 
1191 
1196 
1200  const std::vector<std::vector<std::pair<unsigned int, double>>>
1202 
1208  const unsigned int cell_number_correction_steps;
1209 
1227 
1231  const bool adapt_grids;
1232 
1236  DeclException1(ExcInvalidValue,
1237  double,
1238  << "The value " << arg1
1239  << " for the cell number corridor does not fulfill "
1240  "its natural requirements.");
1241  };
1242 
1243 
1244 
1251  template <int dim>
1253  {
1257  RefinementData(const double refinement_threshold,
1258  const double coarsening_threshold = 0);
1259 
1266  const double refinement_threshold;
1267 
1272  const double coarsening_threshold;
1273 
1277  DeclException1(ExcInvalidValue,
1278  double,
1279  << "The value " << arg1
1280  << " for the cell refinement thresholds does not fulfill "
1281  "its natural requirements.");
1282  };
1283 } // namespace TimeStepBase_Tria_Flags
1284 
1285 
1286 
1303 template <int dim>
1305 {
1306 public:
1312  using RefinementFlags =
1315 
1316 
1322  {
1326  grid_refinement = 0x1000
1327  };
1328 
1329 
1339 
1353  const double time,
1354  const Triangulation<dim, dim> &coarse_grid,
1355  const Flags & flags,
1356  const RefinementFlags & refinement_flags = RefinementFlags());
1357 
1362  virtual ~TimeStepBase_Tria() override;
1363 
1384  virtual void
1385  wake_up(const unsigned int wakeup_level) override;
1386 
1400  virtual void
1401  sleep(const unsigned int) override;
1402 
1417  void
1418  refine_grid(const RefinementData data);
1419 
1425  virtual void
1426  init_for_refinement();
1427 
1435  virtual void
1436  get_tria_refinement_criteria(Vector<float> &criteria) const = 0;
1437 
1442  void
1443  save_refine_flags();
1444 
1453  virtual std::size_t
1454  memory_consumption() const override;
1455 
1459  DeclExceptionMsg(ExcGridNotDeleted,
1460  "When calling restore_grid(), you must have previously "
1461  "deleted the triangulation.");
1462 
1463 protected:
1472 
1480 
1485  const Flags flags;
1486 
1492 
1493 private:
1499  std::vector<std::vector<bool>> refine_flags;
1500 
1504  std::vector<std::vector<bool>> coarsen_flags;
1505 
1510  void
1511  restore_grid();
1512 };
1513 
1514 
1515 
1516 /*--------------------------- template functions ----------------------------*/
1517 
1518 template <typename InitFunctionObject, typename LoopFunctionObject>
1519 void
1520 TimeDependent::do_loop(InitFunctionObject init_function,
1521  LoopFunctionObject loop_function,
1522  const TimeSteppingData &timestepping_data,
1523  const Direction direction)
1524 {
1525  // the following functions looks quite
1526  // disrupted due to the recurring switches
1527  // for forward and backward running loops.
1528  //
1529  // I chose to switch at every place where
1530  // it is needed, since it is so easy
1531  // to overlook something when you change
1532  // some code at one place when it needs
1533  // to be changed at a second place, here
1534  // for the other direction, also.
1535 
1536  const unsigned int n_timesteps = timesteps.size();
1537 
1538  // initialize the time steps for
1539  // a round of this loop
1540  for (unsigned int step = 0; step < n_timesteps; ++step)
1541  switch (direction)
1542  {
1543  case forward:
1544  init_function((&*timesteps[step]));
1545  break;
1546  case backward:
1547  init_function((&*timesteps[n_timesteps - step - 1]));
1548  break;
1549  };
1550 
1551 
1552  // wake up the first few time levels
1553  for (int step = -static_cast<int>(timestepping_data.look_ahead); step < 0;
1554  ++step)
1555  for (int look_ahead = 0;
1556  look_ahead <= static_cast<int>(timestepping_data.look_ahead);
1557  ++look_ahead)
1558  switch (direction)
1559  {
1560  case forward:
1561  if (step + look_ahead >= 0)
1562  timesteps[step + look_ahead]->wake_up(look_ahead);
1563  break;
1564  case backward:
1565  if (n_timesteps - (step + look_ahead) < n_timesteps)
1566  timesteps[n_timesteps - (step + look_ahead)]->wake_up(look_ahead);
1567  break;
1568  };
1569 
1570 
1571  for (unsigned int step = 0; step < n_timesteps; ++step)
1572  {
1573  // first thing: wake up the
1574  // timesteps ahead as necessary
1575  for (unsigned int look_ahead = 0;
1576  look_ahead <= timestepping_data.look_ahead;
1577  ++look_ahead)
1578  switch (direction)
1579  {
1580  case forward:
1581  if (step + look_ahead < n_timesteps)
1582  timesteps[step + look_ahead]->wake_up(look_ahead);
1583  break;
1584  case backward:
1585  if (n_timesteps > (step + look_ahead))
1586  timesteps[n_timesteps - (step + look_ahead) - 1]->wake_up(
1587  look_ahead);
1588  break;
1589  };
1590 
1591 
1592  // actually do the work
1593  switch (direction)
1594  {
1595  case forward:
1596  loop_function((&*timesteps[step]));
1597  break;
1598  case backward:
1599  loop_function((&*timesteps[n_timesteps - step - 1]));
1600  break;
1601  };
1602 
1603  // let the timesteps behind sleep
1604  for (unsigned int look_back = 0; look_back <= timestepping_data.look_back;
1605  ++look_back)
1606  switch (direction)
1607  {
1608  case forward:
1609  if (step >= look_back)
1610  timesteps[step - look_back]->sleep(look_back);
1611  break;
1612  case backward:
1613  if (n_timesteps - (step - look_back) <= n_timesteps)
1614  timesteps[n_timesteps - (step - look_back) - 1]->sleep(
1615  look_back);
1616  break;
1617  }
1618  }
1619 
1620  // make the last few timesteps sleep
1621  for (int step = n_timesteps;
1622  step < static_cast<int>(n_timesteps + timestepping_data.look_back);
1623  ++step)
1624  for (int look_back = 0;
1625  look_back <= static_cast<int>(timestepping_data.look_back);
1626  ++look_back)
1627  switch (direction)
1628  {
1629  case forward:
1630  if ((step - look_back >= 0) &&
1631  (step - look_back < static_cast<int>(n_timesteps)))
1632  timesteps[step - look_back]->sleep(look_back);
1633  break;
1634  case backward:
1635  if ((step - look_back >= 0) &&
1636  (step - look_back < static_cast<int>(n_timesteps)))
1637  timesteps[n_timesteps - (step - look_back) - 1]->sleep(look_back);
1638  break;
1639  };
1640 }
1641 
1643 
1644 /*---------------------------- time-dependent.h ---------------------------*/
1645 #endif
1646 /*---------------------------- 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:515
std::size_t memory_consumption() const
const double time
std::vector< SmartPointer< TimeStepBase, TimeDependent > > timesteps
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:493
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:363
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:362
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)