00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __deal2__time_dependent_h
00013 #define __deal2__time_dependent_h
00014
00015
00016
00017
00018
00019 #include <deal.II/base/config.h>
00020 #include <deal.II/base/exceptions.h>
00021 #include <deal.II/base/subscriptor.h>
00022 #include <deal.II/base/smartpointer.h>
00023
00024 #include <vector>
00025 #include <utility>
00026
00027 DEAL_II_NAMESPACE_OPEN
00028
00029
00030 class TimeStepBase;
00031 template <typename number> class Vector;
00032 template <int dim, int spacedim> class Triangulation;
00033
00349 class TimeDependent
00350 {
00351 public:
00362 struct TimeSteppingData
00363 {
00369 TimeSteppingData (const unsigned int look_ahead,
00370 const unsigned int look_back);
00371
00401 const unsigned int look_ahead;
00402
00423 const unsigned int look_back;
00424 };
00425
00431 enum Direction {
00432 forward, backward
00433 };
00434
00438 TimeDependent (const TimeSteppingData &data_primal,
00439 const TimeSteppingData &data_dual,
00440 const TimeSteppingData &data_postprocess);
00441
00442
00451 virtual ~TimeDependent ();
00452
00485 void insert_timestep (const TimeStepBase *position,
00486 TimeStepBase *new_timestep);
00487
00499 void add_timestep (TimeStepBase *new_timestep);
00500
00525 void delete_timestep (const unsigned int position);
00526
00538 void solve_primal_problem ();
00539
00551 void solve_dual_problem ();
00552
00564 void postprocess ();
00565
00606 template <typename InitFunctionObject, typename LoopFunctionObject>
00607 void do_loop (InitFunctionObject init_function,
00608 LoopFunctionObject loop_function,
00609 const TimeSteppingData ×tepping_data,
00610 const Direction direction);
00611
00612
00640 virtual void start_sweep (const unsigned int sweep_no);
00641
00678 virtual void end_sweep (const unsigned int n_threads = 1);
00679
00685 std::size_t memory_consumption () const;
00686
00690 DeclException0 (ExcInvalidPosition);
00691
00692 protected:
00701 std::vector<SmartPointer<TimeStepBase,TimeDependent> > timesteps;
00702
00708 unsigned int sweep_no;
00709
00716 const TimeSteppingData timestepping_data_primal;
00717
00724 const TimeSteppingData timestepping_data_dual;
00725
00732 const TimeSteppingData timestepping_data_postprocess;
00733
00734 private:
00735
00741 void end_sweep (const unsigned int begin_timestep,
00742 const unsigned int end_timestep);
00743 };
00744
00745
00746
00756 class TimeStepBase : public Subscriptor
00757 {
00758 public:
00763 enum SolutionState {
00764 primal_problem = 0x0,
00765 dual_problem = 0x1,
00766 postprocess = 0x2
00767 };
00768
00773 TimeStepBase (const double time);
00774
00779 virtual ~TimeStepBase ();
00780
00807 virtual void wake_up (const unsigned int);
00808
00826 virtual void sleep (const unsigned int);
00827
00852 virtual void start_sweep ();
00853
00862 virtual void end_sweep ();
00863
00875 virtual void init_for_primal_problem ();
00876
00881 virtual void init_for_dual_problem ();
00882
00887 virtual void init_for_postprocessing ();
00888
00901 virtual void solve_primal_problem () = 0;
00902
00919 virtual void solve_dual_problem ();
00920
00939 virtual void postprocess_timestep ();
00940
00945 double get_time () const;
00946
00953 unsigned int get_timestep_no () const;
00954
00979 double get_backward_timestep () const;
00980
00988 double get_forward_timestep () const;
00989
01003 virtual std::size_t memory_consumption () const;
01004
01008 DeclException0 (ExcCantComputeTimestep);
01009
01010 protected:
01015 const TimeStepBase *previous_timestep;
01016
01021 const TimeStepBase *next_timestep;
01022
01029 unsigned int sweep_no;
01030
01039 unsigned int timestep_no;
01040
01044 const double time;
01045
01055 unsigned int next_action;
01056
01057 private:
01067 void set_previous_timestep (const TimeStepBase *previous);
01068
01078 void set_next_timestep (const TimeStepBase *next);
01079
01090 void set_timestep_no (const unsigned int step_no);
01091
01099 void set_sweep_no (const unsigned int sweep_no);
01100
01101
01111 TimeStepBase (const TimeStepBase &);
01112
01122 TimeStepBase & operator = (const TimeStepBase &);
01123
01124
01125 friend class TimeDependent;
01126 };
01127
01128
01129
01130
01139 namespace TimeStepBase_Tria_Flags
01140 {
01148 template <int dim>
01149 struct Flags
01150 {
01156 Flags ();
01157
01163 Flags (const bool delete_and_rebuild_tria,
01164 const unsigned int wakeup_level_to_build_grid,
01165 const unsigned int sleep_level_to_delete_grid);
01166
01189 const bool delete_and_rebuild_tria;
01190
01208 const unsigned int wakeup_level_to_build_grid;
01209
01216 const unsigned int sleep_level_to_delete_grid;
01217
01221 DeclException1 (ExcInvalidParameter,
01222 int,
01223 << "The parameter " << arg1 << " has an invalid value.");
01224 };
01225
01226
01227
01348 template <int dim>
01349 struct RefinementFlags
01350 {
01359 typedef std::vector<std::vector<std::pair<unsigned int, double> > > CorrectionRelaxations;
01360
01365 static CorrectionRelaxations default_correction_relaxations;
01366
01373 RefinementFlags (const unsigned int max_refinement_level = 0,
01374 const unsigned int first_sweep_with_correction = 0,
01375 const unsigned int min_cells_for_correction = 0,
01376 const double cell_number_corridor_top = (1<<dim),
01377 const double cell_number_corridor_bottom = 1,
01378 const CorrectionRelaxations &correction_relaxations = CorrectionRelaxations(),
01379 const unsigned int cell_number_correction_steps = 0,
01380 const bool mirror_flags_to_previous_grid = false,
01381 const bool adapt_grids = false);
01382
01400 const unsigned int max_refinement_level;
01401
01410 const unsigned int first_sweep_with_correction;
01411
01412
01419 const unsigned int min_cells_for_correction;
01420
01429 const double cell_number_corridor_top;
01430
01434 const double cell_number_corridor_bottom;
01435
01440 const std::vector<std::vector<std::pair<unsigned int,double> > > correction_relaxations;
01441
01450 const unsigned int cell_number_correction_steps;
01451
01485 const bool mirror_flags_to_previous_grid;
01486
01491 const bool adapt_grids;
01492
01496 DeclException1 (ExcInvalidValue,
01497 double,
01498 << "The following value does not fulfill the requirements: " << arg1);
01499 };
01500
01501
01502
01509 template <int dim>
01510 struct RefinementData
01511 {
01515 RefinementData (const double refinement_threshold,
01516 const double coarsening_threshold=0);
01517
01529 const double refinement_threshold;
01530
01537 const double coarsening_threshold;
01538
01542 DeclException1 (ExcInvalidValue,
01543 double,
01544 << "The following value does not fulfill the requirements: " << arg1);
01545 };
01546 }
01547
01548
01549
01550
01568 template <int dim>
01569 class TimeStepBase_Tria : public TimeStepBase
01570 {
01571 public:
01577 typedef typename TimeStepBase_Tria_Flags::Flags<dim> Flags;
01578 typedef typename TimeStepBase_Tria_Flags::RefinementFlags<dim> RefinementFlags;
01579 typedef typename TimeStepBase_Tria_Flags::RefinementData<dim> RefinementData;
01580
01581
01587 enum SolutionState {
01588 grid_refinement = 0x1000
01589 };
01590
01591
01605 TimeStepBase_Tria ();
01606
01629 TimeStepBase_Tria (const double time,
01630 const Triangulation<dim, dim> &coarse_grid,
01631 const Flags &flags,
01632 const RefinementFlags &refinement_flags = RefinementFlags());
01633
01640 virtual ~TimeStepBase_Tria ();
01641
01676 virtual void wake_up (const unsigned int wakeup_level);
01677
01703 virtual void sleep (const unsigned int);
01704
01727 void refine_grid (const RefinementData data);
01728
01737 virtual void init_for_refinement ();
01738
01750 virtual void get_tria_refinement_criteria (Vector<float> &criteria) const = 0;
01751
01759 void save_refine_flags ();
01760
01774 virtual std::size_t memory_consumption () const;
01775
01779 DeclException0 (ExcGridNotDeleted);
01780
01781 protected:
01782
01797 SmartPointer<Triangulation<dim, dim>,TimeStepBase_Tria<dim> > tria;
01798
01808 SmartPointer<const Triangulation<dim, dim>,TimeStepBase_Tria<dim> > coarse_grid;
01809
01816 const Flags flags;
01817
01824 const RefinementFlags refinement_flags;
01825
01826 private:
01834 std::vector<std::vector<bool> > refine_flags;
01835
01839 std::vector<std::vector<bool> > coarsen_flags;
01840
01847 void restore_grid ();
01848 };
01849
01850
01851
01852
01853
01854
01855
01856 template <typename InitFunctionObject, typename LoopFunctionObject>
01857 void TimeDependent::do_loop (InitFunctionObject init_function,
01858 LoopFunctionObject loop_function,
01859 const TimeSteppingData ×tepping_data,
01860 const Direction direction)
01861 {
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873 const unsigned int n_timesteps = timesteps.size();
01874
01875
01876
01877 for (unsigned int step=0; step<n_timesteps; ++step)
01878 switch (direction)
01879 {
01880 case forward:
01881 init_function (static_cast<typename InitFunctionObject::argument_type>
01882 (&*timesteps[step]));
01883 break;
01884 case backward:
01885 init_function (static_cast<typename InitFunctionObject::argument_type>
01886 (&*timesteps[n_timesteps-@ref step_1 "step-1"]));
01887 break;
01888 };
01889
01890
01891
01892 for (int step=-timestepping_data.look_ahead; step<0; ++step)
01893 for (int look_ahead=0;
01894 look_ahead<=static_cast<int>(timestepping_data.look_ahead); ++look_ahead)
01895 switch (direction)
01896 {
01897 case forward:
01898 if (step+look_ahead >= 0)
01899 timesteps[step+look_ahead]->wake_up(look_ahead);
01900 break;
01901 case backward:
01902 if (n_timesteps-(step+look_ahead) < n_timesteps)
01903 timesteps[n_timesteps-(step+look_ahead)]->wake_up(look_ahead);
01904 break;
01905 };
01906
01907
01908 for (unsigned int step=0; step<n_timesteps; ++step)
01909 {
01910
01911
01912 for (unsigned int look_ahead=0;
01913 look_ahead<=timestepping_data.look_ahead; ++look_ahead)
01914 switch (direction)
01915 {
01916 case forward:
01917 if (step+look_ahead < n_timesteps)
01918 timesteps[step+look_ahead]->wake_up(look_ahead);
01919 break;
01920 case backward:
01921 if (n_timesteps > (step+look_ahead))
01922 timesteps[n_timesteps-(step+look_ahead)-1]->wake_up(look_ahead);
01923 break;
01924 };
01925
01926
01927
01928 switch (direction)
01929 {
01930 case forward:
01931 loop_function (static_cast<typename LoopFunctionObject::argument_type>
01932 (&*timesteps[step]));
01933 break;
01934 case backward:
01935 loop_function (static_cast<typename LoopFunctionObject::argument_type>
01936 (&*timesteps[n_timesteps-@ref step_1 "step-1"]));
01937 break;
01938 };
01939
01940
01941 for (unsigned int look_back=0;
01942 look_back<=timestepping_data.look_back; ++look_back)
01943 switch (direction)
01944 {
01945 case forward:
01946 if (step>=look_back)
01947 timesteps[step-look_back]->sleep(look_back);
01948 break;
01949 case backward:
01950 if (n_timesteps-(step-look_back) <= n_timesteps)
01951 timesteps[n_timesteps-(step-look_back)-1]->sleep(look_back);
01952 break;
01953 };
01954 };
01955
01956
01957 for (int step=n_timesteps;
01958 step<static_cast<int>(n_timesteps+timestepping_data.look_back); ++step)
01959 for (int look_back=0;
01960 look_back<=static_cast<int>(timestepping_data.look_back); ++look_back)
01961 switch (direction)
01962 {
01963 case forward:
01964 if ((step-look_back >= 0)
01965 &&
01966 (step-look_back < static_cast<int>(n_timesteps)))
01967 timesteps[step-look_back]->sleep(look_back);
01968 break;
01969 case backward:
01970 if ((step-look_back >= 0)
01971 &&
01972 (step-look_back < static_cast<int>(n_timesteps)))
01973 timesteps[n_timesteps-(step-look_back)-1]->sleep(look_back);
01974 break;
01975 };
01976 }
01977
01978 DEAL_II_NAMESPACE_CLOSE
01979
01980
01981 #endif
01982
01983