tTerminationReason.hh - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
 (HTM) git clone git://src.adamsgaard.dk/pism
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tTerminationReason.hh (4228B)
       ---
            1 // Copyright (C) 2012, 2014, 2015, 2016, 2018  David Maxwell and Constantine Khroulev
            2 //
            3 // This file is part of PISM.
            4 //
            5 // PISM is free software; you can redistribute it and/or modify it under the
            6 // terms of the GNU General Public License as published by the Free Software
            7 // Foundation; either version 3 of the License, or (at your option) any later
            8 // version.
            9 //
           10 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
           11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
           12 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
           13 // details.
           14 //
           15 // You should have received a copy of the GNU General Public License
           16 // along with PISM; if not, write to the Free Software
           17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
           18 
           19 #ifndef TERMINATIONREASON_HH_JW17MC8V
           20 #define TERMINATIONREASON_HH_JW17MC8V
           21 
           22 #include <string>
           23 #include <sstream>
           24 #include <memory>
           25 
           26 #include <petscsnes.h>
           27 #include <petscksp.h>
           28 
           29 namespace pism {
           30 
           31 class TerminationReason {
           32 public:
           33   TerminationReason()
           34     : m_reason(0) {
           35   }
           36 
           37   TerminationReason(int code)
           38     : m_reason(code) {
           39   }
           40 
           41   virtual ~TerminationReason() {
           42   }
           43   
           44   typedef std::shared_ptr<TerminationReason> Ptr;
           45   
           46   virtual int reason() {
           47     return m_reason;
           48   }
           49 
           50   virtual std::string description() {
           51     std::stringstream sdesc;
           52     this->get_description(sdesc);
           53     return sdesc.str();
           54   }
           55   virtual void get_description(std::ostream &desc,int indent_level=0) = 0;
           56 
           57   virtual std::string nested_description(int indent_level=0) {
           58     std::stringstream sdesc;
           59     this->get_nested_description(sdesc,indent_level);
           60     return sdesc.str();
           61   }
           62   virtual void get_nested_description(std::ostream &desc,int indent_level=0) {
           63     this->get_description(desc,indent_level);
           64     if (this->has_root_cause()) {
           65       indent_level++;
           66       desc << std::endl;
           67       this->root_cause()->get_nested_description(desc,indent_level);
           68     }
           69   }
           70 
           71   virtual bool has_root_cause() {
           72     return (bool)m_root_cause;
           73   }
           74 
           75   TerminationReason::Ptr root_cause() {
           76     return m_root_cause;
           77   }
           78 
           79   void set_root_cause(TerminationReason::Ptr cause) {
           80     m_root_cause = cause;
           81   }
           82   
           83   bool succeeded() {
           84     return (this->reason())>0;
           85   }
           86   
           87   bool failed() {
           88     return (this->reason())<0;
           89   }
           90   
           91   bool done() {
           92     return (this->reason())!= 0;
           93   }
           94   
           95 protected:
           96   int m_reason;
           97   TerminationReason::Ptr m_root_cause;
           98   static const char *sm_indent;
           99 
          100 private:
          101   TerminationReason(TerminationReason const &reason);
          102   TerminationReason &operator =(TerminationReason const &reason);
          103 };
          104 
          105 class KSPTerminationReason: public TerminationReason {
          106 public:
          107   KSPTerminationReason(KSPConvergedReason r);
          108   virtual void get_description(std::ostream &desc,int indent_level=0);
          109 };
          110 
          111 class SNESTerminationReason: public TerminationReason {
          112 public:
          113   SNESTerminationReason(SNESConvergedReason r);
          114   virtual void get_description(std::ostream &desc,int indent_level=0);
          115 };
          116 
          117 class GenericTerminationReason: public TerminationReason {
          118 public:
          119   GenericTerminationReason(int code, std::string &desc)
          120     : TerminationReason(code), m_description(desc) {
          121   }
          122 
          123   GenericTerminationReason(int code, const std::string &desc)
          124     : TerminationReason(code), m_description(desc) {
          125   }
          126 
          127   virtual ~GenericTerminationReason() {
          128     // empty
          129   }
          130   
          131   static TerminationReason::Ptr keep_iterating() {
          132     static TerminationReason::Ptr sm_keep_iterating(new GenericTerminationReason(0,"Keep iterating."));
          133     return sm_keep_iterating;
          134   }
          135 
          136   static TerminationReason::Ptr max_iter() {
          137     static TerminationReason::Ptr sm_max_iter(new GenericTerminationReason(-1,"Iteration count exceeded."));
          138     return sm_max_iter;
          139   }
          140 
          141   static TerminationReason::Ptr success() {
          142     static TerminationReason::Ptr sm_success(new GenericTerminationReason(1,"Success."));
          143     return sm_success;
          144   }
          145 
          146   static TerminationReason::Ptr failure() {
          147     static TerminationReason::Ptr sm_failure(new GenericTerminationReason(-1,"Failure."));
          148     return sm_failure;
          149   }
          150 
          151   virtual void get_description(std::ostream &desc, int indent_level=0); 
          152 protected:
          153   std::string m_description;
          154 };
          155 
          156 } // end of namespace pism
          157 
          158 #endif /* end of include guard: TERMINATIONREASON_HH_JW17MC8V */