deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\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\}}\)
Loading...
Searching...
No Matches
solver_control.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 1998 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_solver_control_h
14#define dealii_solver_control_h
15
16
17#include <deal.II/base/config.h>
18
21
22#include <vector>
23
25
26#ifndef DOXYGEN
28#endif
29
66{
67public:
72 enum State
73 {
80 };
81
82
83
94 {
95 public:
96 NoConvergence(const unsigned int last_step, const double last_residual)
99 {}
100
101 virtual ~NoConvergence() noexcept override = default;
102
103 virtual void
104 print_info(std::ostream &out) const override
105 {
106 out
107 << "Iterative method reported convergence failure in step " << last_step
108 << ". The residual in the last step was " << last_residual << ".\n\n"
109 << "This error message can indicate that you have simply not allowed "
110 << "a sufficiently large number of iterations for your iterative solver "
111 << "to converge. This often happens when you increase the size of your "
112 << "problem. In such cases, the last residual will likely still be very "
113 << "small, and you can make the error go away by increasing the allowed "
114 << "number of iterations when setting up the SolverControl object that "
115 << "determines the maximal number of iterations you allow."
116 << "\n\n"
117 << "The other situation where this error may occur is when your matrix "
118 << "is not invertible (e.g., your matrix has a null-space), or if you "
119 << "try to apply the wrong solver to a matrix (e.g., using CG for a "
120 << "matrix that is not symmetric or not positive definite). In these "
121 << "cases, the residual in the last iteration is likely going to be large."
122 << std::endl;
123 }
124
128 const unsigned int last_step;
129
133 const double last_residual;
134 };
135
136
137
149 explicit SolverControl(const unsigned int n = 100,
150 const double tol = 1.e-10,
151 const bool log_history = false,
152 const bool log_result = false);
153
158 virtual ~SolverControl() override = default;
159
163 static void
165
169 void
171
189 virtual State
190 check(const unsigned int step, const double check_value);
191
195 State
196 last_check() const;
197
201 double
202 initial_value() const;
203
208 double
209 last_value() const;
210
214 unsigned int
215 last_step() const;
216
220 unsigned int
221 max_steps() const;
222
226 unsigned int
227 set_max_steps(const unsigned int);
228
234 void
235 set_failure_criterion(const double rel_failure_residual);
236
241 void
243
247 double
248 tolerance() const;
249
253 double
254 set_tolerance(const double);
255
260 void
262
271 const std::vector<double> &
272 get_history_data() const;
273
279 double
280 average_reduction() const;
287 double
288 final_reduction() const;
289
295 double
296 step_reduction(unsigned int step) const;
297
301 void
302 log_history(const bool);
303
307 bool
308 log_history() const;
309
313 unsigned int
314 log_frequency(unsigned int);
315
319 void
320 log_result(const bool);
321
325 bool
326 log_result() const;
327
334
335protected:
339 unsigned int maxsteps;
340
344 double tol;
345
350
355
359 double lvalue;
360
364 unsigned int lstep;
365
371
376
384
389
393 unsigned int m_log_frequency;
394
401
406
413 std::vector<double> history_data;
414};
415
416
429{
430public:
436 explicit ReductionControl(const unsigned int maxiter = 100,
437 const double tolerance = 1.e-10,
438 const double reduce = 1.e-2,
439 const bool log_history = false,
440 const bool log_result = false);
441
446 explicit ReductionControl(const SolverControl &c);
447
453 operator=(const SolverControl &c);
454
459 virtual ~ReductionControl() override = default;
460
464 static void
466
470 void
472
478 virtual State
479 check(const unsigned int step, const double check_value) override;
480
484 double
485 reduction() const;
486
490 double
491 set_reduction(const double);
492
493protected:
497 double reduce;
498
504};
505
517{
518public:
523 explicit IterationNumberControl(const unsigned int maxiter = 100,
524 const double tolerance = 1e-12,
525 const bool log_history = false,
526 const bool log_result = false);
527
533
541
546 virtual ~IterationNumberControl() override = default;
547
553 virtual State
554 check(const unsigned int step, const double check_value) override;
555};
556
557
570{
571public:
578 explicit ConsecutiveControl(const unsigned int maxiter = 100,
579 const double tolerance = 1.e-10,
580 const unsigned int n_consecutive_iterations = 2,
581 const bool log_history = false,
582 const bool log_result = false);
583
588 explicit ConsecutiveControl(const SolverControl &c);
589
596 operator=(const SolverControl &c);
597
602 virtual ~ConsecutiveControl() override = default;
603
608 virtual State
609 check(const unsigned int step, const double check_value) override;
610
611protected:
617
622};
623
625//---------------------------------------------------------------------------
626
627#ifndef DOXYGEN
628
629inline unsigned int
631{
632 return maxsteps;
633}
634
635
636
637inline unsigned int
638SolverControl::set_max_steps(const unsigned int newval)
639{
640 unsigned int old = maxsteps;
641 maxsteps = newval;
642 return old;
643}
644
645
646
647inline void
648SolverControl::set_failure_criterion(const double rel_failure_residual)
649{
650 relative_failure_residual = rel_failure_residual;
651 check_failure = true;
652}
653
654
655
656inline void
658{
661 check_failure = false;
662}
663
664
665
666inline double
668{
669 return tol;
670}
671
672
673
674inline double
675SolverControl::set_tolerance(const double t)
676{
677 double old = tol;
678 tol = t;
679 return old;
680}
681
682
683inline void
684SolverControl::log_history(const bool newval)
685{
686 m_log_history = newval;
687}
688
689
690
691inline bool
693{
694 return m_log_history;
695}
696
697
698inline void
699SolverControl::log_result(const bool newval)
700{
701 m_log_result = newval;
702}
703
704
705inline bool
707{
708 return m_log_result;
709}
710
711
712inline double
714{
715 return reduce;
716}
717
718
719inline double
721{
722 double old = reduce;
723 reduce = t;
724 return old;
725}
726
727#endif // DOXYGEN
728
730
731#endif
ConsecutiveControl & operator=(const SolverControl &c)
virtual ~ConsecutiveControl() override=default
unsigned int n_consecutive_iterations
unsigned int n_converged_iterations
virtual State check(const unsigned int step, const double check_value) override
IterationNumberControl & operator=(const SolverControl &c)
virtual State check(const unsigned int step, const double check_value) override
IterationNumberControl(const SolverControl &c)
virtual ~IterationNumberControl() override=default
double set_reduction(const double)
void parse_parameters(ParameterHandler &param)
virtual ~ReductionControl() override=default
double reduction() const
virtual State check(const unsigned int step, const double check_value) override
static void declare_parameters(ParameterHandler &param)
ReductionControl & operator=(const SolverControl &c)
NoConvergence(const unsigned int last_step, const double last_residual)
virtual void print_info(std::ostream &out) const override
virtual ~NoConvergence() noexcept override=default
void log_history(const bool)
static void declare_parameters(ParameterHandler &param)
State last_check() const
double average_reduction() const
virtual ~SolverControl() override=default
const std::vector< double > & get_history_data() const
void clear_failure_criterion()
unsigned int lstep
unsigned int last_step() const
double last_value() const
void log_result(const bool)
unsigned int m_log_frequency
unsigned int log_frequency(unsigned int)
virtual State check(const unsigned int step, const double check_value)
unsigned int maxsteps
void set_failure_criterion(const double rel_failure_residual)
bool log_history() const
void enable_history_data()
double step_reduction(unsigned int step) const
std::vector< double > history_data
double failure_residual
double relative_failure_residual
unsigned int set_max_steps(const unsigned int)
double set_tolerance(const double)
unsigned int max_steps() const
double initial_value() const
double tolerance() const
bool log_result() const
void parse_parameters(ParameterHandler &param)
double final_reduction() const
@ iterate
Continue iteration.
@ success
Stop iteration, goal reached.
@ failure
Stop iteration, goal not reached.
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DeclException0(Exception0)
static ::ExceptionBase & ExcHistoryDataRequired()
STL namespace.