Reference documentation for deal.II version 8.5.1
timestep_control.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2006 - 2016 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/algorithms/timestep_control.h>
18 #include <deal.II/base/parameter_handler.h>
19 
20 DEAL_II_NAMESPACE_OPEN
21 
22 using namespace Algorithms;
23 
25  double final,
26  double tolerance,
27  double start_step,
28  double print_step,
29  double max_step)
30  : start_val(start),
31  final_val(final),
32  tolerance_val(tolerance),
33  strategy_val(uniform),
34  start_step_val(start_step),
35  max_step_val(max_step),
36  min_step_val(0),
37  current_step_val(start_step),
38  step_val(start_step),
39  print_step(print_step),
40  next_print_val(print_step > 0. ? start_val + print_step : start_val - 1.)
41 {
42  now_val = start_val;
43  strcpy(format, "T.%06.3f");
44 
45  // avoid compiler warning
46  (void)min_step_val;
47 }
48 
49 
50 
52 {
53  param.declare_entry ("Start", "0.", Patterns::Double());
54  param.declare_entry ("Final", "1.", Patterns::Double());
55  param.declare_entry ("First step", "1.e-2", Patterns::Double());
56  param.declare_entry ("Max step", "1.", Patterns::Double());
57  param.declare_entry ("Tolerance", "1.e-2", Patterns::Double());
58  param.declare_entry ("Print step", "-1.", Patterns::Double());
59  param.declare_entry ("Strategy", "uniform",
60  Patterns::Selection("uniform|doubling"));
61 }
62 
63 
64 
65 
67 {
68  start (param.get_double ("Start"));
69  start_step (param.get_double ("First step"));
70  max_step (param.get_double ("Max step"));
71  final (param.get_double ("Final"));
72  tolerance (param.get_double ("Tolerance"));
73  print_step = param.get_double ("Print step");
74  const std::string strat = param.get("Strategy");
75  if (strat == std::string("uniform"))
76  strategy_val = uniform;
77  else if (strat == std::string("doubling"))
78  strategy_val = doubling;
79 }
80 
81 
82 
83 
84 bool
86 {
87  bool changed = false;
88  double s = step_val;
89 
90  // Do time step control, but not in
91  // first step.
92  if (now_val != start())
93  {
94  if (strategy_val == doubling && 2*s <= tolerance_val)
95  s *= 2;
96  if (s > max_step_val)
97  s = max_step_val;
98  }
99 
100  // Try incrementing time by s
101  double h = now_val + s;
102  changed = s != step_val;
103 
104  step_val = s;
105  current_step_val = s;
106  // If we just missed the final
107  // time, increase the step size a
108  // bit. This way, we avoid a very
109  // small final step. If the step
110  // shot over the final time, adjust
111  // it so we hit the final time
112  // exactly.
113  double s1 = .01*s;
114  if (h > final_val-s1)
115  {
116  current_step_val = final_val - now_val;
117  h = final_val;
118  changed = true;
119  }
120 
121  now_val = h;
122  return changed;
123 }
124 
125 
127 {
128  if (print_step == 0.)
129  return false;
130  if (print_step < 0.)
131  return true;
132 
133  bool result = (now_val >= next_print_val);
134 
135  if (result)
136  {
137  next_print_val += print_step;
138  if (next_print_val > final_val)
139  next_print_val = final_val;
140  }
141  return result;
142 }
143 
144 DEAL_II_NAMESPACE_CLOSE
145 
std::string get(const std::string &entry_string) const
TimestepControl(double start=0., double final=1., double tolerance=1.e-2, double start_step=1.e-2, double print_step=-1., double max_step=1.)
void start_step(const double step)
double get_double(const std::string &entry_name) const
static void declare_parameters(ParameterHandler &param)
void parse_parameters(ParameterHandler &param)
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())