File: timestep_control.cc

package info (click to toggle)
deal.ii 9.7.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 326,024 kB
  • sloc: cpp: 440,899; ansic: 77,337; python: 3,307; perl: 1,041; sh: 1,022; xml: 252; makefile: 97; javascript: 14
file content (122 lines) | stat: -rw-r--r-- 3,325 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2010 - 2024 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------


#include <deal.II/algorithms/timestep_control.h>

#include <deal.II/base/parameter_handler.h>

DEAL_II_NAMESPACE_OPEN

namespace Algorithms
{
  TimestepControl::TimestepControl(double start,
                                   double final,
                                   double tolerance,
                                   double start_step,
                                   double print_step,
                                   double max_step)
    : start_val(start)
    , final_val(final)
    , tolerance_val(tolerance)
    , start_step_val(start_step)
    , max_step_val(max_step)
    , min_step_val(0)
    , current_step_val(start_step)
    , step_val(start_step)
    , print_step(print_step)
    , next_print_val(print_step > 0. ? start_val + print_step : start_val - 1.)
  {
    now_val = start_val;

    // avoid compiler warning
    (void)min_step_val;
  }



  void
  TimestepControl::declare_parameters(ParameterHandler &param)
  {
    param.declare_entry("Start", "0.", Patterns::Double());
    param.declare_entry("Final", "1.", Patterns::Double());
    param.declare_entry("First step", "1.e-2", Patterns::Double(0.));
    param.declare_entry("Max step", "1.", Patterns::Double(0.));
    param.declare_entry("Tolerance", "1.e-2", Patterns::Double(0.));
    param.declare_entry("Print step", "-1.", Patterns::Double());
  }



  void
  TimestepControl::parse_parameters(const ParameterHandler &param)
  {
    start(param.get_double("Start"));
    start_step(param.get_double("First step"));
    max_step(param.get_double("Max step"));
    final(param.get_double("Final"));
    tolerance(param.get_double("Tolerance"));
    print_step = param.get_double("Print step");
  }



  bool
  TimestepControl::advance()
  {
    bool changed = false;

    // Try incrementing time by s
    double now_trial = now_val + step_val;
    current_step_val = step_val;

    // If we just missed the final time, increase the step size a bit. This way,
    // we avoid a very small final step. If the step shot over the final time,
    // adjust it so we hit the final time exactly.
    double s1 = .01 * step_val;
    if (now_trial > final_val - s1)
      {
        current_step_val = final_val - now_val;
        now_trial        = final_val;
        changed          = true;
      }

    now_val = now_trial;
    return changed;
  }


  bool
  TimestepControl::print()
  {
    if (print_step == 0.)
      return false;
    if (print_step < 0.)
      return true;

    bool result = (now_val >= next_print_val);

    if (result)
      {
        next_print_val += print_step;
        if (next_print_val > final_val)
          next_print_val = final_val;
      }
    return result;
  }

} // namespace Algorithms


DEAL_II_NAMESPACE_CLOSE