PISM, A Parallel Ice Sheet Model  stable v2.1.1 committed by Constantine Khrulev on 2024-12-04 13:36:58 -0900
TerminationReason.hh
Go to the documentation of this file.
1 // Copyright (C) 2012, 2014, 2015, 2016, 2018, 2023 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 
32 public:
34  : m_reason(0) {
35  }
36 
38  : m_reason(code) {
39  }
40 
41  virtual ~TerminationReason() {
42  }
43 
44  virtual int reason() {
45  return m_reason;
46  }
47 
48  virtual std::string description() {
49  std::stringstream sdesc;
50  this->get_description(sdesc);
51  return sdesc.str();
52  }
53  virtual void get_description(std::ostream &desc,int indent_level=0) = 0;
54 
55  virtual std::string nested_description(int indent_level=0) {
56  std::stringstream sdesc;
57  this->get_nested_description(sdesc,indent_level);
58  return sdesc.str();
59  }
60  virtual void get_nested_description(std::ostream &desc,int indent_level=0) {
61  this->get_description(desc,indent_level);
62  if (this->has_root_cause()) {
63  indent_level++;
64  desc << std::endl;
65  this->root_cause()->get_nested_description(desc,indent_level);
66  }
67  }
68 
69  virtual bool has_root_cause() {
70  return (bool)m_root_cause;
71  }
72 
73  std::shared_ptr<TerminationReason> root_cause() {
74  return m_root_cause;
75  }
76 
77  void set_root_cause(std::shared_ptr<TerminationReason> cause) {
78  m_root_cause = cause;
79  }
80 
81  bool succeeded() {
82  return (this->reason())>0;
83  }
84 
85  bool failed() {
86  return (this->reason())<0;
87  }
88 
89  bool done() {
90  return (this->reason())!= 0;
91  }
92 
93 protected:
94  int m_reason;
95  std::shared_ptr<TerminationReason> m_root_cause;
96  static const char *sm_indent;
97 
98 private:
101 };
102 
104 public:
105  KSPTerminationReason(KSPConvergedReason r);
106  virtual void get_description(std::ostream &desc,int indent_level=0);
107 };
108 
110 public:
111  SNESTerminationReason(SNESConvergedReason r);
112  virtual void get_description(std::ostream &desc,int indent_level=0);
113 };
114 
116 public:
117  GenericTerminationReason(int code, std::string &desc)
118  : TerminationReason(code), m_description(desc) {
119  }
120 
121  GenericTerminationReason(int code, const std::string &desc)
122  : TerminationReason(code), m_description(desc) {
123  }
124 
126  // empty
127  }
128 
129  static std::shared_ptr<TerminationReason> keep_iterating() {
130  static std::shared_ptr<TerminationReason> sm_keep_iterating(new GenericTerminationReason(0,"Keep iterating."));
131  return sm_keep_iterating;
132  }
133 
134  static std::shared_ptr<TerminationReason> max_iter() {
135  static std::shared_ptr<TerminationReason> sm_max_iter(new GenericTerminationReason(-1,"Iteration count exceeded."));
136  return sm_max_iter;
137  }
138 
139  static std::shared_ptr<TerminationReason> success() {
140  static std::shared_ptr<TerminationReason> sm_success(new GenericTerminationReason(1,"Success."));
141  return sm_success;
142  }
143 
144  static std::shared_ptr<TerminationReason> failure() {
145  static std::shared_ptr<TerminationReason> sm_failure(new GenericTerminationReason(-1,"Failure."));
146  return sm_failure;
147  }
148 
149  virtual void get_description(std::ostream &desc, int indent_level=0);
150 protected:
151  std::string m_description;
152 };
153 
154 } // end of namespace pism
155 
156 #endif /* end of include guard: TERMINATIONREASON_HH_JW17MC8V */
virtual void get_description(std::ostream &desc, int indent_level=0)
GenericTerminationReason(int code, std::string &desc)
static std::shared_ptr< TerminationReason > max_iter()
static std::shared_ptr< TerminationReason > keep_iterating()
static std::shared_ptr< TerminationReason > success()
static std::shared_ptr< TerminationReason > failure()
GenericTerminationReason(int code, const std::string &desc)
KSPTerminationReason(KSPConvergedReason r)
virtual void get_description(std::ostream &desc, int indent_level=0)
virtual void get_description(std::ostream &desc, int indent_level=0)
SNESTerminationReason(SNESConvergedReason r)
virtual void get_nested_description(std::ostream &desc, int indent_level=0)
virtual void get_description(std::ostream &desc, int indent_level=0)=0
std::shared_ptr< TerminationReason > m_root_cause
void set_root_cause(std::shared_ptr< TerminationReason > cause)
virtual std::string description()
virtual std::string nested_description(int indent_level=0)
static const char * sm_indent
TerminationReason & operator=(TerminationReason const &reason)
std::shared_ptr< TerminationReason > root_cause()
TerminationReason(TerminationReason const &reason)