16 #include <deal.II/numerics/time_dependent.h> 17 #include <deal.II/base/memory_consumption.h> 18 #include <deal.II/base/thread_management.h> 19 #include <deal.II/base/utilities.h> 20 #include <deal.II/base/parallel.h> 21 #include <deal.II/grid/tria.h> 22 #include <deal.II/grid/tria_accessor.h> 23 #include <deal.II/grid/tria_iterator.h> 24 #include <deal.II/grid/grid_refinement.h> 25 #include <deal.II/lac/vector.h> 31 DEAL_II_NAMESPACE_OPEN
34 const unsigned int look_back)
36 look_ahead (look_ahead),
68 (position ==
nullptr),
73 if (position ==
nullptr)
79 timesteps.back()->set_next_timestep (new_timestep);
91 timesteps[0]->set_previous_timestep (new_timestep);
100 const std::vector<SmartPointer<TimeStepBase,TimeDependent> >::iterator insert_position
107 (*(insert_position-1))->set_next_timestep (new_timestep);
110 (*insert_position)->set_previous_timestep (new_timestep);
157 timesteps[position]->set_previous_timestep ((position!=0) ?
205 for (
unsigned int step=0; step<
timesteps.size(); ++step)
211 for (
unsigned int step=0; step<
timesteps.size(); ++step)
219 void (
TimeDependent::*p) (
const unsigned int,
const unsigned int)
222 std::bind (p,
this, std::placeholders::_1, std::placeholders::_2),
229 const unsigned int end)
231 for (
unsigned int step=begin; step<end; ++step)
245 for (
unsigned int i=0; i<
timesteps.size(); ++i)
257 previous_timestep(nullptr),
258 next_timestep (nullptr),
259 sweep_no (
numbers::invalid_unsigned_int),
260 timestep_no (
numbers::invalid_unsigned_int),
262 next_action(
numbers::invalid_unsigned_int)
351 ExcMessage(
"The backward time step cannot be computed because " 352 "there is no previous time step."));
362 ExcMessage(
"The forward time step cannot be computed because " 363 "there is no next time step."));
405 return sizeof(*this);
413 tria (nullptr, typeid(*this).name()),
414 coarse_grid (nullptr, typeid(*this).name()),
427 const RefinementFlags &refinement_flags) :
429 tria(nullptr, typeid(*this).name()),
430 coarse_grid (&coarse_grid, typeid(*this).name()),
432 refinement_flags (refinement_flags)
440 if (!flags.delete_and_rebuild_tria)
449 coarse_grid =
nullptr;
460 if (wakeup_level == flags.wakeup_level_to_build_grid)
461 if (flags.delete_and_rebuild_tria || !tria)
471 if (sleep_level == flags.sleep_level_to_delete_grid)
475 if (flags.delete_and_rebuild_tria)
493 refine_flags.emplace_back ();
494 coarsen_flags.emplace_back ();
495 tria->save_refine_flags (refine_flags.back());
496 tria->save_coarsen_flags (coarsen_flags.back());
504 Assert (tria ==
nullptr, ExcGridNotDeleted());
505 Assert (refine_flags.size() == coarsen_flags.size(),
515 for (
unsigned int previous_sweep=0; previous_sweep<refine_flags.size();
519 tria->load_refine_flags (refine_flags[previous_sweep]);
520 tria->load_coarsen_flags (coarsen_flags[previous_sweep]);
535 tria->execute_coarsening_and_refinement ();
571 if (new_cell->active())
573 if (new_cell->refine_flag_set() && old_cell->active())
575 if (old_cell->coarsen_flag_set())
576 old_cell->clear_coarsen_flag();
578 old_cell->set_refine_flag();
584 if (old_cell->has_children() && new_cell->has_children())
587 for (
unsigned int c=0; c<new_cell->n_children(); ++c)
588 ::mirror_refinement_flags<dim> (new_cell->child(c), old_cell->child(c));
600 if (cell2->has_children() && cell1->has_children())
602 bool grids_changed =
false;
605 for (
unsigned int c=0; c<cell1->n_children(); ++c)
606 grids_changed |= ::adapt_grid_cells<dim> (cell1->child(c),
608 return grids_changed;
612 if (!cell1->has_children() && !cell2->has_children())
618 if (cell1->refine_flag_set() && cell2->coarsen_flag_set())
620 cell2->clear_coarsen_flag();
623 else if (cell1->coarsen_flag_set() && cell2->refine_flag_set())
625 cell1->clear_coarsen_flag();
633 if (cell1->has_children() && !cell2->has_children())
652 bool changed_grid =
false;
653 if (cell2->coarsen_flag_set())
655 cell2->clear_coarsen_flag();
659 if (!cell2->refine_flag_set())
660 for (
unsigned int c=0; c<cell1->n_children(); ++c)
661 if (cell1->child(c)->refine_flag_set() ||
662 cell1->child(c)->has_children())
664 cell2->set_refine_flag();
671 if (!cell1->has_children() && cell2->has_children())
674 bool changed_grid =
false;
675 if (cell1->coarsen_flag_set())
677 cell1->clear_coarsen_flag();
681 if (!cell1->refine_flag_set())
682 for (
unsigned int c=0; c<cell2->n_children(); ++c)
683 if (cell2->child(c)->refine_flag_set() ||
684 cell2->child(c)->has_children())
686 cell1->set_refine_flag();
704 bool grids_changed =
false;
707 cell2 = tria2.
begin();
712 for (; cell1!=endc; ++cell1, ++cell2)
713 grids_changed |= ::adapt_grid_cells<dim> (cell1, cell2);
715 return grids_changed;
723 Vector<float> criteria;
724 get_tria_refinement_criteria (criteria);
739 Vector<float> sorted_criteria;
743 Vector<float>::const_iterator p_refinement_threshold=
nullptr,
744 p_coarsening_threshold=
nullptr;
761 if ((timestep_no != 0) &&
762 (sweep_no>=refinement_flags.first_sweep_with_correction) &&
763 (refinement_flags.cell_number_correction_steps > 0))
765 sorted_criteria = criteria;
766 std::sort (sorted_criteria.begin(),
767 sorted_criteria.end());
769 sorted_criteria.end(),
770 static_cast<float>(refinement_threshold));
771 p_coarsening_threshold = std::upper_bound (sorted_criteria.begin(),
772 sorted_criteria.end(),
773 static_cast<float>(coarsening_threshold));
784 const unsigned int n_active_cells = tria->n_active_cells ();
825 if ((timestep_no != 0) && (sweep_no>=refinement_flags.first_sweep_with_correction))
826 for (
unsigned int loop=0;
827 loop<refinement_flags.cell_number_correction_steps; ++
loop)
835 if (refinement_flags.adapt_grids)
836 ::adapt_grids<dim> (*previous_tria, *tria);
841 tria->prepare_coarsening_and_refinement ();
842 previous_tria->prepare_coarsening_and_refinement ();
862 double previous_cells = previous_tria->n_active_cells();
865 endc = previous_tria->
end();
866 for (; cell!=endc; ++cell)
867 if (cell->refine_flag_set())
869 else if (cell->coarsen_flag_set())
889 double estimated_cells = n_active_cells;
892 for (; cell!=endc; ++cell)
893 if (cell->refine_flag_set())
895 else if (cell->coarsen_flag_set())
902 double delta_up = refinement_flags.cell_number_corridor_top,
903 delta_down = refinement_flags.cell_number_corridor_bottom;
905 const std::vector<std::pair<unsigned int,double> > &relaxations
906 = (sweep_no >= refinement_flags.correction_relaxations.size() ?
907 refinement_flags.correction_relaxations.back() :
908 refinement_flags.correction_relaxations[sweep_no]);
909 for (
unsigned int r=0; r!=relaxations.size(); ++r)
910 if (n_active_cells < relaxations[r].first)
912 delta_up *= relaxations[r].second;
913 delta_down *= relaxations[r].second;
926 if (estimated_cells > previous_cells*(1.+delta_up))
944 if (estimated_cells>refinement_flags.min_cells_for_correction)
948 double delta_cells = estimated_cells -
949 previous_cells*(1.+delta_up);
960 for (
unsigned int i=0; i<delta_cells;
962 if (p_refinement_threshold != sorted_criteria.end())
963 ++p_refinement_threshold;
982 if (estimated_cells < previous_cells*(1.-delta_down))
986 double delta_cells = previous_cells*(1.-delta_down)-estimated_cells;
1012 if (
loop != refinement_flags.cell_number_correction_steps-1)
1021 for (
unsigned int i=0; i<delta_cells;
1023 if (p_refinement_threshold != p_coarsening_threshold)
1024 --refinement_threshold;
1025 else if (p_coarsening_threshold != sorted_criteria.begin())
1026 --p_coarsening_threshold, --p_refinement_threshold;
1035 if (p_refinement_threshold == sorted_criteria.end())
1037 Assert (p_coarsening_threshold != p_refinement_threshold,
1039 --p_refinement_threshold;
1042 coarsening_threshold = *p_coarsening_threshold;
1043 refinement_threshold = *p_refinement_threshold;
1045 if (coarsening_threshold>=refinement_threshold)
1046 coarsening_threshold = 0.999*refinement_threshold;
1054 for (; cell!=endc; ++cell)
1056 cell->clear_refine_flag ();
1057 cell->clear_coarsen_flag ();
1073 if ((timestep_no >= 1) && (refinement_flags.adapt_grids))
1090 if (refinement_flags.mirror_flags_to_previous_grid)
1092 ::adapt_grids<dim> (*previous_tria, *tria);
1095 old_cell = previous_tria->
begin(0);
1096 new_cell = tria->begin(0);
1097 endc = tria->end(0);
1098 for (; new_cell!=endc; ++new_cell, ++old_cell)
1099 ::mirror_refinement_flags<dim> (new_cell, old_cell);
1102 tria->prepare_coarsening_and_refinement ();
1110 ::adapt_grids<dim> (*previous_tria, *tria);
1119 next_action = grid_refinement;
1131 sizeof(flags) +
sizeof(refinement_flags) +
1141 delete_and_rebuild_tria (false),
1142 wakeup_level_to_build_grid (0),
1143 sleep_level_to_delete_grid (0)
1152 const unsigned int wakeup_level_to_build_grid,
1153 const unsigned int sleep_level_to_delete_grid):
1154 delete_and_rebuild_tria (delete_and_rebuild_tria),
1155 wakeup_level_to_build_grid (wakeup_level_to_build_grid),
1156 sleep_level_to_delete_grid (sleep_level_to_delete_grid)
1169 std::vector<std::pair<unsigned int,double> >(1,
1172 std::make_pair (0U, 0.)));
1178 const unsigned int first_sweep_with_correction,
1179 const unsigned int min_cells_for_correction,
1180 const double cell_number_corridor_top,
1181 const double cell_number_corridor_bottom,
1183 const unsigned int cell_number_correction_steps,
1184 const bool mirror_flags_to_previous_grid,
1185 const bool adapt_grids) :
1186 max_refinement_level(max_refinement_level),
1187 first_sweep_with_correction (first_sweep_with_correction),
1188 min_cells_for_correction(min_cells_for_correction),
1189 cell_number_corridor_top(cell_number_corridor_top),
1190 cell_number_corridor_bottom(cell_number_corridor_bottom),
1191 correction_relaxations (correction_relaxations.size() != 0 ?
1192 correction_relaxations :
1193 default_correction_relaxations),
1194 cell_number_correction_steps(cell_number_correction_steps),
1195 mirror_flags_to_previous_grid(mirror_flags_to_previous_grid),
1196 adapt_grids(adapt_grids)
1207 const double _coarsening_threshold) :
1208 refinement_threshold(_refinement_threshold),
1227 coarsening_threshold((_coarsening_threshold == _refinement_threshold ?
1228 _coarsening_threshold :
1229 0.999*_coarsening_threshold))
1244 #include "time_dependent.inst" 1247 DEAL_II_NAMESPACE_CLOSE
void set_sweep_no(const unsigned int sweep_no)
virtual std::size_t memory_consumption() const
virtual void solve_primal_problem()=0
Iterator lower_bound(Iterator first, Iterator last, const T &val)
virtual void sleep(const unsigned int)
virtual void start_sweep()
static ::ExceptionBase & ExcPureFunctionCalled()
virtual void copy_triangulation(const Triangulation< dim, spacedim > &other_tria)
void loop(ITERATOR begin, typename identity< ITERATOR >::type end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
#define AssertNothrow(cond, exc)
void refine_grid(const RefinementData data)
static ::ExceptionBase & ExcInvalidValue(double arg1)
TimeDependent(const TimeSteppingData &data_primal, const TimeSteppingData &data_dual, const TimeSteppingData &data_postprocess)
virtual void solve_dual_problem()
void set_timestep_no(const unsigned int step_no)
void solve_primal_problem()
void add_timestep(TimeStepBase *new_timestep)
void solve_dual_problem()
const TimeStepBase * next_timestep
const TimeSteppingData timestepping_data_postprocess
double get_forward_timestep() const
void set_next_timestep(const TimeStepBase *next)
const TimeStepBase * previous_timestep
active_cell_iterator begin_active(const unsigned int level=0) const
const TimeSteppingData timestepping_data_dual
cell_iterator begin(const unsigned int level=0) const
void apply_to_subranges(const RangeType &begin, const typename identity< RangeType >::type &end, const Function &f, const unsigned int grainsize)
unsigned int n_levels() const
const double cell_number_corridor_top
void set_previous_timestep(const TimeStepBase *previous)
cell_iterator end() const
unsigned int get_timestep_no() const
void do_loop(InitFunctionObject init_function, LoopFunctionObject loop_function, const TimeSteppingData ×tepping_data, const Direction direction)
virtual bool prepare_coarsening_and_refinement()
static ::ExceptionBase & ExcMessage(std::string arg1)
TriaIterator< CellAccessor< dim, spacedim > > cell_iterator
std::size_t memory_consumption() const
virtual void init_for_refinement()
RefinementData(const double refinement_threshold, const double coarsening_threshold=0)
#define Assert(cond, exc)
const TimeSteppingData timestepping_data_primal
static ::ExceptionBase & ExcInvalidPosition()
virtual void start_sweep(const unsigned int sweep_no)
virtual void init_for_postprocessing()
virtual ~TimeStepBase_Tria()
void coarsen(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold)
static CorrectionRelaxations default_correction_relaxations
static ::ExceptionBase & ExcInvalidValue(double arg1)
double get_backward_timestep() const
const double coarsening_threshold
void delete_timestep(const unsigned int position)
virtual std::size_t memory_consumption() const
virtual void wake_up(const unsigned int)
RefinementFlags(const unsigned int max_refinement_level=0, const unsigned int first_sweep_with_correction=0, const unsigned int min_cells_for_correction=0, const double cell_number_corridor_top=(1<< dim), const double cell_number_corridor_bottom=1, const CorrectionRelaxations &correction_relaxations=CorrectionRelaxations(), const unsigned int cell_number_correction_steps=0, const bool mirror_flags_to_previous_grid=false, const bool adapt_grids=false)
virtual void sleep(const unsigned int)
std::vector< SmartPointer< TimeStepBase, TimeDependent > > timesteps
virtual void init_for_dual_problem()
const double cell_number_corridor_bottom
virtual void init_for_primal_problem()
static ::ExceptionBase & ExcNotImplemented()
void insert_timestep(const TimeStepBase *position, TimeStepBase *new_timestep)
const double refinement_threshold
virtual void postprocess_timestep()
std::vector< std::vector< std::pair< unsigned int, double > > > CorrectionRelaxations
void refine(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
virtual void wake_up(const unsigned int wakeup_level)
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
TimeStepBase(const double time)
TimeSteppingData(const unsigned int look_ahead, const unsigned int look_back)
static ::ExceptionBase & ExcInternalError()