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