Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
solver_control.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 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 #include <deal.II/base/logstream.h>
19 
21 
22 #include <cmath>
23 #include <sstream>
24 
26 
27 /*----------------------- SolverControl ---------------------------------*/
28 
29 
30 SolverControl::SolverControl(const unsigned int maxiter,
31  const double tolerance,
32  const bool m_log_history,
33  const bool m_log_result)
34  : maxsteps(maxiter)
35  , tol(tolerance)
36  , lcheck(failure)
37  , initial_val(numbers::signaling_nan<double>())
38  , lvalue(numbers::signaling_nan<double>())
40  , check_failure(false)
41  , relative_failure_residual(0)
42  , failure_residual(0)
43  , m_log_history(m_log_history)
44  , m_log_frequency(1)
45  , m_log_result(m_log_result)
46  , history_data_enabled(false)
47 {}
48 
49 
50 
52 SolverControl::check(const unsigned int step, const double check_value)
53 {
54  // if this is the first time we
55  // come here, then store the
56  // residual for later comparisons
57  if (step == 0)
58  {
59  initial_val = check_value;
60  }
61 
62  if (m_log_history && ((step % m_log_frequency) == 0))
63  deallog << "Check " << step << "\t" << check_value << std::endl;
64 
65  lstep = step;
66  lvalue = check_value;
67 
68  if (step == 0)
69  {
70  if (check_failure)
72 
73  if (m_log_result)
74  deallog << "Starting value " << check_value << std::endl;
75  }
76 
78  history_data.push_back(check_value);
79 
80  if (check_value <= tol)
81  {
82  if (m_log_result)
83  deallog << "Convergence step " << step << " value " << check_value
84  << std::endl;
85  lcheck = success;
86  return success;
87  }
88 
89  if ((step >= maxsteps) || std::isnan(check_value) ||
90  (check_failure && (check_value > failure_residual)))
91  {
92  if (m_log_result)
93  deallog << "Failure step " << step << " value " << check_value
94  << std::endl;
95  lcheck = failure;
96  return failure;
97  }
98 
99  lcheck = iterate;
100  return iterate;
101 }
102 
103 
104 
107 {
108  return lcheck;
109 }
110 
111 
112 double
114 {
115  return initial_val;
116 }
117 
118 
119 double
121 {
122  return lvalue;
123 }
124 
125 
126 unsigned int
128 {
129  return lstep;
130 }
131 
132 
133 unsigned int
135 {
136  if (f == 0)
137  f = 1;
138  unsigned int old = m_log_frequency;
139  m_log_frequency = f;
140  return old;
141 }
142 
143 
144 void
146 {
147  history_data_enabled = true;
148 }
149 
150 
151 
152 const std::vector<double> &
154 {
156  Assert(
157  history_data.size() > 0,
158  ExcMessage(
159  "The SolverControl object was asked for the solver history "
160  "data, but there is no data. Possibly you requested the data before the "
161  "solver was run."));
162 
163  return history_data;
164 }
165 
166 
167 
168 double
170 {
171  if (lstep == 0)
172  return 0.;
173 
178 
179  return std::pow(history_data[lstep] / history_data[0], 1. / lstep);
180 }
181 
182 
183 
184 double
185 SolverControl::step_reduction(unsigned int step) const
186 {
189  Assert(step <= lstep, ExcIndexRange(step, 1, lstep + 1));
190  Assert(step > 0, ExcIndexRange(step, 1, lstep + 1));
191 
192  return history_data[step] / history_data[step - 1];
193 }
194 
195 
196 double
198 {
199  return step_reduction(lstep);
200 }
201 
202 
203 void
205 {
206  param.declare_entry("Max steps", "100", Patterns::Integer());
207  param.declare_entry("Tolerance", "1.e-10", Patterns::Double());
208  param.declare_entry("Log history", "false", Patterns::Bool());
209  param.declare_entry("Log frequency", "1", Patterns::Integer());
210  param.declare_entry("Log result", "true", Patterns::Bool());
211 }
212 
213 
214 void
216 {
217  set_max_steps(param.get_integer("Max steps"));
218  set_tolerance(param.get_double("Tolerance"));
219  log_history(param.get_bool("Log history"));
220  log_result(param.get_bool("Log result"));
221  log_frequency(param.get_integer("Log frequency"));
222 }
223 
224 /*----------------------- ReductionControl ---------------------------------*/
225 
226 
228  const double tol,
229  const double red,
230  const bool m_log_history,
231  const bool m_log_result)
232  : SolverControl(n, tol, m_log_history, m_log_result)
233  , reduce(red)
234  , reduced_tol(numbers::signaling_nan<double>())
235 {}
236 
237 
239  : SolverControl(c)
240  , reduce(numbers::signaling_nan<double>())
241  , reduced_tol(numbers::signaling_nan<double>())
242 {
243  set_reduction(0.);
244 }
245 
246 
249 {
251  set_reduction(0.);
252  return *this;
253 }
254 
255 
256 
258 ReductionControl::check(const unsigned int step, const double check_value)
259 {
260  // if this is the first time we
261  // come here, then store the
262  // residual for later comparisons
263  if (step == 0)
264  {
265  initial_val = check_value;
266  reduced_tol = check_value * reduce;
267  }
268 
269  // check whether desired reduction
270  // has been achieved. also check
271  // for equality in case initial
272  // residual already was zero
273  if (check_value <= reduced_tol)
274  {
275  if (m_log_result)
276  deallog << "Convergence step " << step << " value " << check_value
277  << std::endl;
278  lstep = step;
279  lvalue = check_value;
280  lcheck = success;
281 
283  history_data.push_back(check_value);
284 
285  return success;
286  }
287  else
288  return SolverControl::check(step, check_value);
289 }
290 
291 
292 
293 void
295 {
297  param.declare_entry("Reduction", "1.e-2", Patterns::Double());
298 }
299 
300 
301 void
303 {
305  set_reduction(param.get_double("Reduction"));
306 }
307 
308 
309 /*---------------------- IterationNumberControl -----------------------------*/
310 
311 
313  const double tolerance,
314  const bool m_log_history,
315  const bool m_log_result)
316  : SolverControl(n, tolerance, m_log_history, m_log_result)
317 {}
318 
319 
320 
322 IterationNumberControl::check(const unsigned int step, const double check_value)
323 {
324  // check whether the given number of iterations was reached, and return
325  // success in that case. Otherwise, go on to the check of the base class.
326  if (step >= this->maxsteps)
327  {
328  if (m_log_result)
329  deallog << "Convergence step " << step << " value " << check_value
330  << std::endl;
331  lstep = step;
332  lvalue = check_value;
333 
334  lcheck = success;
335  return success;
336  }
337  else
338  return SolverControl::check(step, check_value);
339 }
340 
341 /*------------------------ ConsecutiveControl -------------------------------*/
342 
343 
345  const unsigned int n,
346  const double tolerance,
347  const unsigned int n_consecutive_iterations,
348  const bool m_log_history,
349  const bool m_log_result)
350  : SolverControl(n, tolerance, m_log_history, m_log_result)
351  , n_consecutive_iterations(n_consecutive_iterations)
352  , n_converged_iterations(0)
353 {
355  ExcMessage("n_consecutive_iterations should be positive"));
356 }
357 
358 
359 
361  : SolverControl(c)
362  , n_consecutive_iterations(1)
363  , n_converged_iterations(0)
364 {}
365 
366 
367 
370 {
374  return *this;
375 }
376 
377 
378 
380 ConsecutiveControl::check(const unsigned int step, const double check_value)
381 {
382  // reset the counter if ConsecutiveControl is being reused
383  if (step == 0)
385  else
386  {
387  // check two things:
388  // (i) steps are ascending without repetitions
389  // (ii) user started from zero even when solver is being reused.
390  Assert(step - 1 == lstep,
391  ExcMessage("steps should be ascending integers."));
392  }
393 
394  SolverControl::State state = SolverControl::check(step, check_value);
395  // check if we need to override the success:
396  if (state == success)
397  {
400  {
401  return success;
402  }
403  else
404  {
405  lcheck = iterate;
406  return iterate;
407  }
408  }
409  else
410  {
412  return state;
413  }
414 }
415 
ReductionControl::reduced_tol
double reduced_tol
Definition: solver_control.h:502
SolverControl::m_log_frequency
unsigned int m_log_frequency
Definition: solver_control.h:390
SolverControl::check
virtual State check(const unsigned int step, const double check_value)
Definition: solver_control.cc:52
SolverControl::m_log_history
bool m_log_history
Definition: solver_control.h:385
ParameterHandler::get_double
double get_double(const std::string &entry_name) const
Definition: parameter_handler.cc:1056
IterationNumberControl::IterationNumberControl
IterationNumberControl(const unsigned int maxiter=100, const double tolerance=1e-12, const bool log_history=false, const bool log_result=true)
Definition: solver_control.cc:312
SolverControl::State
State
Definition: solver_control.h:74
SolverControl::last_check
State last_check() const
Definition: solver_control.cc:106
ParameterHandler::get_bool
bool get_bool(const std::string &entry_name) const
Definition: parameter_handler.cc:1101
SolverControl::final_reduction
double final_reduction() const
Definition: solver_control.cc:197
ReductionControl::check
virtual State check(const unsigned int step, const double check_value) override
Definition: solver_control.cc:258
ParameterHandler::declare_entry
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation="", const bool has_to_be_set=false)
Definition: parameter_handler.cc:784
ReductionControl::set_reduction
double set_reduction(const double)
SolverControl::enable_history_data
void enable_history_data()
Definition: solver_control.cc:145
Subscriptor::operator=
Subscriptor & operator=(const Subscriptor &)
Definition: subscriptor.h:283
SolverControl::get_history_data
const std::vector< double > & get_history_data() const
Definition: solver_control.cc:153
ReductionControl::operator=
ReductionControl & operator=(const SolverControl &c)
Definition: solver_control.cc:248
SolverControl::ExcHistoryDataRequired
static ::ExceptionBase & ExcHistoryDataRequired()
Patterns::Bool
Definition: patterns.h:984
SolverControl::declare_parameters
static void declare_parameters(ParameterHandler &param)
Definition: solver_control.cc:204
SolverControl::last_value
double last_value() const
Definition: solver_control.cc:120
ReductionControl
Definition: solver_control.h:427
ConsecutiveControl::n_converged_iterations
unsigned int n_converged_iterations
Definition: solver_control.h:624
SolverControl::average_reduction
double average_reduction() const
Definition: solver_control.cc:169
SolverControl::failure_residual
double failure_residual
Definition: solver_control.h:380
SolverControl::lcheck
State lcheck
Definition: solver_control.h:346
deallog
LogStream deallog
Definition: logstream.cc:37
SolverControl::last_step
unsigned int last_step() const
Definition: solver_control.cc:127
SolverControl::iterate
@ iterate
Continue iteration.
Definition: solver_control.h:77
SolverControl::SolverControl
SolverControl(const unsigned int n=100, const double tol=1.e-10, const bool log_history=false, const bool log_result=true)
Definition: solver_control.cc:30
SolverControl::set_max_steps
unsigned int set_max_steps(const unsigned int)
SolverControl::log_frequency
unsigned int log_frequency(unsigned int)
Definition: solver_control.cc:134
SolverControl::history_data
std::vector< double > history_data
Definition: solver_control.h:410
StandardExceptions::ExcIndexRange
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
StandardExceptions::ExcMessage
static ::ExceptionBase & ExcMessage(std::string arg1)
double
ParameterHandler::get_integer
long int get_integer(const std::string &entry_string) const
Definition: parameter_handler.cc:1013
ReductionControl::parse_parameters
void parse_parameters(ParameterHandler &param)
Definition: solver_control.cc:302
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
SolverControl::tol
double tol
Definition: solver_control.h:341
numbers::signaling_nan
T signaling_nan()
Definition: signaling_nan.h:229
SolverControl::step_reduction
double step_reduction(unsigned int step) const
Definition: solver_control.cc:185
parameter_handler.h
SolverControl::history_data_enabled
bool history_data_enabled
Definition: solver_control.h:402
ReductionControl::ReductionControl
ReductionControl(const unsigned int maxiter=100, const double tolerance=1.e-10, const double reduce=1.e-2, const bool log_history=false, const bool log_result=true)
Definition: solver_control.cc:227
numbers
Definition: numbers.h:207
ReductionControl::reduce
double reduce
Definition: solver_control.h:496
ConsecutiveControl
Definition: solver_control.h:572
SolverControl::parse_parameters
void parse_parameters(ParameterHandler &param)
Definition: solver_control.cc:215
ConsecutiveControl::n_consecutive_iterations
unsigned int n_consecutive_iterations
Definition: solver_control.h:619
StandardExceptions::ExcInternalError
static ::ExceptionBase & ExcInternalError()
SolverControl::lstep
unsigned int lstep
Definition: solver_control.h:361
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
SolverControl::m_log_result
bool m_log_result
Definition: solver_control.h:397
ConsecutiveControl::ConsecutiveControl
ConsecutiveControl(const unsigned int maxiter=100, const double tolerance=1.e-10, const unsigned int n_consecutive_iterations=2, const bool log_history=false, const bool log_result=false)
Definition: solver_control.cc:344
SolverControl::log_history
bool log_history() const
SolverControl::failure
@ failure
Stop iteration, goal not reached.
Definition: solver_control.h:81
SolverControl::lvalue
double lvalue
Definition: solver_control.h:356
numbers::invalid_unsigned_int
static const unsigned int invalid_unsigned_int
Definition: types.h:191
ReductionControl::declare_parameters
static void declare_parameters(ParameterHandler &param)
Definition: solver_control.cc:294
signaling_nan.h
IterationNumberControl::check
virtual State check(const unsigned int step, const double check_value) override
Definition: solver_control.cc:322
SolverControl::success
@ success
Stop iteration, goal reached.
Definition: solver_control.h:79
ParameterHandler
Definition: parameter_handler.h:845
ConsecutiveControl::operator=
ConsecutiveControl & operator=(const SolverControl &c)
Definition: solver_control.cc:369
SolverControl::relative_failure_residual
double relative_failure_residual
Definition: solver_control.h:372
SolverControl::check_failure
bool check_failure
Definition: solver_control.h:367
SolverControl
Definition: solver_control.h:67
SolverControl::log_result
bool log_result() const
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
logstream.h
SolverControl::set_tolerance
double set_tolerance(const double)
SolverControl::initial_val
double initial_val
Definition: solver_control.h:351
AssertThrow
#define AssertThrow(cond, exc)
Definition: exceptions.h:1531
ConsecutiveControl::check
virtual State check(const unsigned int step, const double check_value) override
Definition: solver_control.cc:380
Patterns::Double
Definition: patterns.h:293
SolverControl::initial_value
double initial_value() const
Definition: solver_control.cc:113
solver_control.h
SolverControl::maxsteps
unsigned int maxsteps
Definition: solver_control.h:336
Patterns::Integer
Definition: patterns.h:190