include/deal.II/lac/solver_control.h

00001 //---------------------------------------------------------------------------
00002 //    @f$Id: solver_control.h 25345 2012-03-31 08:37:04Z bangerth @f$
00003 //
00004 //    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2010, 2012 by the deal.II authors
00005 //
00006 //    This file is subject to QPL and may not be  distributed
00007 //    without copyright and license information. Please refer
00008 //    to the file deal.II/doc/license.html for the  text  and
00009 //    further information on this license.
00010 //
00011 //---------------------------------------------------------------------------
00012 #ifndef __deal2__solver_control_h
00013 #define __deal2__solver_control_h
00014 
00015 
00016 #include <deal.II/base/config.h>
00017 #include <deal.II/base/subscriptor.h>
00018 
00019 #include <vector>
00020 
00021 DEAL_II_NAMESPACE_OPEN
00022 
00023 class ParameterHandler;
00024 
00027 
00061 class SolverControl : public Subscriptor
00062 {
00063   public:
00064 
00072     enum State {
00074       iterate = 0,
00076       success,
00078       failure
00079     };
00080 
00099     class NoConvergence : public std::exception
00100     {
00101       public:
00105         NoConvergence (const unsigned int last_step,
00106                        const double       last_residual);
00107 
00112         virtual const char * what () const throw ();
00113 
00118         const unsigned int last_step;
00119 
00123         const double       last_residual;
00124     };
00125 
00126 
00147     SolverControl (const unsigned int n           = 100,
00148                    const double       tol         = 1.e-10,
00149                    const bool         log_history = false,
00150                    const bool         log_result  = true);
00151 
00157     virtual ~SolverControl();
00158 
00162     static void declare_parameters (ParameterHandler& param);
00163 
00167     void parse_parameters (ParameterHandler& param);
00168 
00213     virtual State check (const unsigned int step,
00214                          const double   check_value);
00215 
00219     State last_check() const;
00220 
00225     double initial_value() const;
00226 
00232     double last_value() const;
00233 
00237     unsigned int last_step() const;
00238 
00242     unsigned int max_steps () const;
00243 
00247     unsigned int set_max_steps (const unsigned int);
00248 
00256     void set_failure_criterion (const double rel_failure_residual);
00257 
00265     void clear_failure_criterion ();
00266 
00270     double tolerance () const;
00271 
00275     double set_tolerance (const double);
00276 
00282     void enable_history_data();
00283 
00291     double average_reduction() const;
00302     double final_reduction() const;
00303 
00311     double step_reduction(unsigned int step) const;
00312 
00318     void log_history (const bool);
00319 
00323     bool log_history () const;
00324 
00328     unsigned int log_frequency (unsigned int);
00329 
00333     void log_result (const bool);
00334 
00338     bool log_result () const;
00339 
00349     DeclException0(ExcHistoryDataRequired);
00350 
00351   protected:
00355     unsigned int maxsteps;
00356 
00360     double       tol;
00361 
00365     State        lcheck;
00366 
00370     double initial_val;
00371 
00375     double       lvalue;
00376 
00380     unsigned int lstep;
00381 
00387     bool         check_failure;
00388 
00389                                      /*
00390                                       * Stores the
00391                                       * @p rel_failure_residual set by
00392                                       * @p set_failure_criterion
00393                                       */
00394     double       relative_failure_residual;
00395 
00405     double       failure_residual;
00406 
00411     bool         m_log_history;
00415     unsigned int m_log_frequency;
00416 
00425     bool         m_log_result;
00426 
00432     bool history_data_enabled;
00433 
00442     std::vector<double> history_data;
00443 };
00444 
00445 
00460 class ReductionControl : public SolverControl
00461 {
00462   public:
00471     ReductionControl (const unsigned int maxiter = 100,
00472                       const double   tolerance   = 1.e-10,
00473                       const double   reduce      = 1.e-2,
00474                       const bool     log_history = false,
00475                       const bool     log_result  = true);
00476 
00484     ReductionControl (const SolverControl& c);
00485 
00493     ReductionControl& operator= (const SolverControl& c);
00494 
00500     virtual ~ReductionControl();
00501 
00505     static void declare_parameters (ParameterHandler& param);
00506 
00510     void parse_parameters (ParameterHandler& param);
00511 
00520     virtual State check (const unsigned int step,
00521                          const double   check_value);
00522 
00526     double reduction () const;
00527 
00531     double set_reduction (const double);
00532 
00533   protected:
00537     double reduce;
00538 
00545     double reduced_tol;
00546 };
00547 
00549 //---------------------------------------------------------------------------
00550 
00551 #ifndef DOXYGEN
00552 
00553 inline unsigned int
00554 SolverControl::max_steps () const
00555 {
00556   return maxsteps;
00557 }
00558 
00559 
00560 
00561 inline unsigned int
00562 SolverControl::set_max_steps (const unsigned int newval)
00563 {
00564   unsigned int old = maxsteps;
00565   maxsteps = newval;
00566   return old;
00567 }
00568 
00569 
00570 
00571 inline void
00572 SolverControl::set_failure_criterion (const double rel_failure_residual)
00573 {
00574   relative_failure_residual=rel_failure_residual;
00575   check_failure=true;
00576 }
00577 
00578 
00579 
00580 inline void
00581 SolverControl::clear_failure_criterion ()
00582 {
00583   relative_failure_residual=0;
00584   failure_residual=0;
00585   check_failure=false;
00586 }
00587 
00588 
00589 
00590 inline double
00591 SolverControl::tolerance () const
00592 {
00593   return tol;
00594 }
00595 
00596 
00597 
00598 inline double
00599 SolverControl::set_tolerance (const double t)
00600 {
00601   double old = tol;
00602   tol = t;
00603   return old;
00604 }
00605 
00606 
00607 inline void
00608 SolverControl::log_history (const bool newval)
00609 {
00610   m_log_history = newval;
00611 }
00612 
00613 
00614 
00615 inline bool
00616 SolverControl::log_history () const
00617 {
00618   return m_log_history;
00619 }
00620 
00621 
00622 
00623 inline void
00624 SolverControl::log_result (const bool newval)
00625 {
00626   m_log_result = newval;
00627 }
00628 
00629 
00630 
00631 inline bool
00632 SolverControl::log_result () const
00633 {
00634   return m_log_result;
00635 }
00636 
00637 
00638 inline double
00639 ReductionControl::reduction () const
00640 {
00641   return reduce;
00642 }
00643 
00644 
00645 
00646 inline double
00647 ReductionControl::set_reduction (const double t)
00648 {
00649   double old = reduce;
00650   reduce = t;
00651   return old;
00652 }
00653 
00654 #endif // DOXYGEN
00655 
00656 DEAL_II_NAMESPACE_CLOSE
00657 
00658 #endif
00659 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Wed May 23 2012 06:07:39 by doxygen 1.7.3