Belos  Version of the Day
BelosTypes.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 #include <BelosTypes.hpp>
43 
44 namespace Belos {
45 
46  namespace {
47  const char*
48  convertStatusTypeToRawString (const StatusType status)
49  {
50  if (status == Passed) {
51  return "Passed";
52  } else if (status == Failed) {
53  return "Failed";
54  } else if (status == Undefined) {
55  return "Undefined";
56  } else {
57  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
58  "Belos::convertStatusTypeToRawString: Invalid StatusType enum value "
59  << status << ".");
60  }
61  }
62  } // namespace (anonymous)
63 
64  const char* TEUCHOS_DEPRECATED
65  toString (const StatusType status)
66  {
67  return convertStatusTypeToRawString (status);
68  }
69 
70  std::string
72  {
73  return convertStatusTypeToRawString (status);
74  }
75 
77  convertStringToStatusType (const std::string& status)
78  {
79  if (status == "Passed") {
80  return Passed;
81  } else if (status == "Failed") {
82  return Failed;
83  } else if (status == "Undefined") {
84  return Undefined;
85  } else {
86  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
87  "Belos::convertStringToStatusType: Invalid string \"" << status
88  << "\".");
89  }
90  }
91 
92  ScaleType
93  convertStringToScaleType (const std::string& scaleType)
94  {
95  if (scaleType == "Norm of Initial Residual") {
96  return Belos::NormOfInitRes;
97  } else if (scaleType == "Norm of Preconditioned Initial Residual") {
99  } else if (scaleType == "Norm of RHS") {
100  return Belos::NormOfRHS;
101  } else if (scaleType == "None") {
102  return Belos::None;
103  } else if (scaleType == "User Provided") {
104  return Belos::UserProvided;
105  } else {
106  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
107  "Belos::convertStringToScaleType(): Invalid residual scaling type \""
108  << scaleType << "\".");
109  }
110  }
111 
112  std::string
114  {
115  if (scaleType == Belos::NormOfInitRes) {
116  return "Norm of Initial Residual";
117  } else if (scaleType == Belos::NormOfPrecInitRes) {
118  return "Norm of Preconditioned Initial Residual";
119  } else if (scaleType == Belos::NormOfRHS) {
120  return "Norm of RHS";
121  } else if (scaleType == Belos::None) {
122  return "None";
123  } else if (scaleType == Belos::UserProvided) {
124  return "User Provided";
125  } else {
126  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
127  "Belos::convertScaleTypeToString(): Invalid residual scaling type "
128  "value " << scaleType << ".");
129  }
130  }
131 
132  std::string
134  {
135  typedef std::vector<int>::size_type size_type;
136 
137  // Wouldn't it be nice if C++ enums had introspection and could
138  // be enumerated?
139  const size_type numValidTypes = 8;
140  const int validTypes[] = {
149  };
150  const char* typeNames[] = {
151  "Errors",
152  "Warnings",
153  "IterationDetails",
154  "OrthoDetails",
155  "FinalSummary",
156  "TimingDetails",
157  "StatusTestDetails",
158  "Debug"
159  };
160 
161  // We first generate a list, and only then build a single string.
162  // This helps us decide where to put the commas. The list just
163  // uses the indices of the valid names, rather than the valid
164  // names themselves, in order to save space and time. We use
165  // size_type for the indices to avoid signed/unsigned comparisons.
166  std::vector<size_type> theList;
167  for (size_type nameIndex = 0; nameIndex < numValidTypes; ++nameIndex) {
168  if (msgType & validTypes[nameIndex]) {
169  theList.push_back (nameIndex);
170  }
171  }
172  std::ostringstream os;
173  for (size_type k = 0; k < theList.size(); ++k) {
174  const size_type nameIndex = theList[k];
175  os << typeNames[nameIndex];
176  if (nameIndex < theList.size() - 1) {
177  os << ",";
178  }
179  }
180  return os.str();
181  }
182 
183  std::string
185  {
186  if (result == Belos::Converged) {
187  return "Converged";
188  } else if (result == Belos::Unconverged) {
189  return "Unconverged";
190  } else {
191  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
192  "Belos::convertReturnTypeToString: Invalid ReturnType enum value "
193  << result << ".");
194  }
195  }
196 
197 } // end Belos namespace
198 
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
Definition: BelosTypes.cpp:93
Collection of types and exceptions used within the Belos solvers.
ScaleType
The type of scaling to use on the residual norm value.
Definition: BelosTypes.hpp:119
MsgType
Available message types recognized by the linear solvers.
Definition: BelosTypes.hpp:252
StatusType
Whether the StatusTest wants iteration to stop.
Definition: BelosTypes.hpp:183
std::string convertStatusTypeToString(const StatusType status)
The string name corresponding to the given StatusType enum value.
Definition: BelosTypes.cpp:71
std::string convertScaleTypeToString(const ScaleType scaleType)
Convert the given ScaleType enum value to its corresponding string.
Definition: BelosTypes.cpp:113
std::string convertMsgTypeToString(const MsgType msgType)
Show MsgType as a comma-delimited list of names.
Definition: BelosTypes.cpp:133
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:149
std::string convertReturnTypeToString(const ReturnType result)
Convert the given ReturnType enum value to its corresponding string.
Definition: BelosTypes.cpp:184
const char *TEUCHOS_DEPRECATED toString(const StatusType status)
The string name corresponding to the given StatusType enum value.
Definition: BelosTypes.cpp:65
StatusType convertStringToStatusType(const std::string &status)
The StatusType enum value corresponding to the given string name.
Definition: BelosTypes.cpp:77