Loading [MathJax]/extensions/tex2jax.js
PISM, A Parallel Ice Sheet Model 2.2.2-d6b3a29ca committed by Constantine Khrulev on 2025-03-28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
29namespace pism {
30
32public:
34 : m_reason(0) {
35 }
36
38 : m_reason(code) {
39 }
40
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
93protected:
95 std::shared_ptr<TerminationReason> m_root_cause;
96 static const char *sm_indent;
97
98private:
101};
102
104public:
105 KSPTerminationReason(KSPConvergedReason r);
106 virtual void get_description(std::ostream &desc,int indent_level=0);
107};
108
110public:
111 SNESTerminationReason(SNESConvergedReason r);
112 virtual void get_description(std::ostream &desc,int indent_level=0);
113};
114
116public:
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);
150protected:
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)
static std::shared_ptr< TerminationReason > max_iter()
GenericTerminationReason(int code, std::string &desc)
static std::shared_ptr< TerminationReason > keep_iterating()
static std::shared_ptr< TerminationReason > failure()
static std::shared_ptr< TerminationReason > success()
GenericTerminationReason(int code, const std::string &desc)
virtual void get_description(std::ostream &desc, int indent_level=0)
virtual void get_description(std::ostream &desc, int indent_level=0)
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()
std::shared_ptr< TerminationReason > root_cause()
virtual std::string nested_description(int indent_level=0)
TerminationReason & operator=(TerminationReason const &reason)
static const char * sm_indent
TerminationReason(TerminationReason const &reason)