Reference documentation for deal.II version 8.4.2
timestep_control.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2006 - 2014 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 {
41  now_val = start_val;
42  strcpy(format, "T.%06.3f");
43 
44  // avoid compiler warning
45  (void)min_step_val;
46 }
47 
48 
49 
51 {
52  param.declare_entry ("Start", "0.", Patterns::Double());
53  param.declare_entry ("Final", "1.", Patterns::Double());
54  param.declare_entry ("First step", "1.e-2", Patterns::Double());
55  param.declare_entry ("Max step", "1.", Patterns::Double());
56  param.declare_entry ("Tolerance", "1.e-2", Patterns::Double());
57  param.declare_entry ("Print step", "-1.", Patterns::Double());
58  param.declare_entry ("Strategy", "uniform",
59  Patterns::Selection("uniform|doubling"));
60 }
61 
62 
63 
64 
66 {
67  start (param.get_double ("Start"));
68  start_step (param.get_double ("First step"));
69  max_step (param.get_double ("Max step"));
70  final (param.get_double ("Final"));
71  tolerance (param.get_double ("Tolerance"));
72  print_step = param.get_double ("Print step");
73  const std::string strat = param.get("Strategy");
74  if (strat == std::string("uniform"))
75  strategy_val = uniform;
76  else if (strat == std::string("doubling"))
77  strategy_val = doubling;
78 }
79 
80 
81 
82 
83 bool
85 {
86  bool changed = false;
87  double s = step_val;
88 
89  // Do time step control, but not in
90  // first step.
91  if (now_val != start())
92  {
93  if (strategy_val == doubling && 2*s <= tolerance_val)
94  s *= 2;
95  if (s > max_step_val)
96  s = max_step_val;
97  }
98 
99  // Try incrementing time by s
100  double h = now_val + s;
101  changed = s != step_val;
102 
103  step_val = s;
104  current_step_val = s;
105  // If we just missed the final
106  // time, increase the step size a
107  // bit. This way, we avoid a very
108  // small final step. If the step
109  // shot over the final time, adjust
110  // it so we hit the final time
111  // exactly.
112  double s1 = .01*s;
113  if (h > final_val-s1)
114  {
115  current_step_val = final_val - now_val;
116  h = final_val;
117  changed = true;
118  }
119 
120  now_val = h;
121  return changed;
122 }
123 
124 
126 {
127  if (print_step == 0.)
128  return false;
129  if (print_step < 0.)
130  return true;
131 
132  bool result = (now_val >= next_print_val);
133 
134  if (result)
135  {
136  next_print_val += print_step;
137  if (next_print_val > final_val)
138  next_print_val = final_val;
139  }
140  return result;
141 }
142 
143 DEAL_II_NAMESPACE_CLOSE
144 
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())