Belos Package Browser (Single Doxygen Collection)  Development
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 #include <vector>
44 
45 namespace Belos {
46 
47  namespace {
48  const char*
49  convertStatusTypeToRawString (const StatusType status)
50  {
51  if (status == Passed) {
52  return "Passed";
53  } else if (status == Failed) {
54  return "Failed";
55  } else if (status == Undefined) {
56  return "Undefined";
57  } else {
58  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
59  "Belos::convertStatusTypeToRawString: Invalid StatusType enum value "
60  << status << ".");
61  }
62  }
63  } // namespace (anonymous)
64 
65  std::string
67  {
68  return convertStatusTypeToRawString (status);
69  }
70 
72  convertStringToStatusType (const std::string& status)
73  {
74  if (status == "Passed") {
75  return Passed;
76  } else if (status == "Failed") {
77  return Failed;
78  } else if (status == "Undefined") {
79  return Undefined;
80  } else {
81  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
82  "Belos::convertStringToStatusType: Invalid string \"" << status
83  << "\".");
84  }
85  }
86 
87  ScaleType
88  convertStringToScaleType (const std::string& scaleType)
89  {
90  if (scaleType == "Norm of Initial Residual") {
91  return Belos::NormOfInitRes;
92  } else if (scaleType == "Norm of Preconditioned Initial Residual") {
94  } else if (scaleType == "Norm of RHS") {
95  return Belos::NormOfRHS;
96  } else if (scaleType == "None") {
97  return Belos::None;
98  } else if (scaleType == "User Provided") {
99  return Belos::UserProvided;
100  } else {
101  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
102  "Belos::convertStringToScaleType(): Invalid residual scaling type \""
103  << scaleType << "\".");
104  }
105  }
106 
107  std::string
109  {
110  if (scaleType == Belos::NormOfInitRes) {
111  return "Norm of Initial Residual";
112  } else if (scaleType == Belos::NormOfPrecInitRes) {
113  return "Norm of Preconditioned Initial Residual";
114  } else if (scaleType == Belos::NormOfRHS) {
115  return "Norm of RHS";
116  } else if (scaleType == Belos::None) {
117  return "None";
118  } else if (scaleType == Belos::UserProvided) {
119  return "User Provided";
120  } else {
121  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
122  "Belos::convertScaleTypeToString(): Invalid residual scaling type "
123  "value " << scaleType << ".");
124  }
125  }
126 
127  std::string
129  {
130  typedef std::vector<int>::size_type size_type;
131 
132  // Wouldn't it be nice if C++ enums had introspection and could
133  // be enumerated?
134  const size_type numValidTypes = 8;
135  const int validTypes[] = {
144  };
145  const char* typeNames[] = {
146  "Errors",
147  "Warnings",
148  "IterationDetails",
149  "OrthoDetails",
150  "FinalSummary",
151  "TimingDetails",
152  "StatusTestDetails",
153  "Debug"
154  };
155 
156  // We first generate a list, and only then build a single string.
157  // This helps us decide where to put the commas. The list just
158  // uses the indices of the valid names, rather than the valid
159  // names themselves, in order to save space and time. We use
160  // size_type for the indices to avoid signed/unsigned comparisons.
161  std::vector<size_type> theList;
162  for (size_type nameIndex = 0; nameIndex < numValidTypes; ++nameIndex) {
163  if (msgType & validTypes[nameIndex]) {
164  theList.push_back (nameIndex);
165  }
166  }
167  std::ostringstream os;
168  for (size_type k = 0; k < theList.size(); ++k) {
169  const size_type nameIndex = theList[k];
170  os << typeNames[nameIndex];
171  if (nameIndex < theList.size() - 1) {
172  os << ",";
173  }
174  }
175  return os.str();
176  }
177 
178  std::string
180  {
181  if (result == Belos::Converged) {
182  return "Converged";
183  } else if (result == Belos::Unconverged) {
184  return "Unconverged";
185  } else {
186  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
187  "Belos::convertReturnTypeToString: Invalid ReturnType enum value "
188  << result << ".");
189  }
190  }
191 
192 } // end Belos namespace
193 
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
Definition: BelosTypes.cpp:88
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:257
StatusType
Whether the StatusTest wants iteration to stop.
Definition: BelosTypes.hpp:188
std::string convertStatusTypeToString(const StatusType status)
The string name corresponding to the given StatusType enum value.
Definition: BelosTypes.cpp:66
std::string convertScaleTypeToString(const ScaleType scaleType)
Convert the given ScaleType enum value to its corresponding string.
Definition: BelosTypes.cpp:108
std::string convertMsgTypeToString(const MsgType msgType)
Show MsgType as a comma-delimited list of names.
Definition: BelosTypes.cpp:128
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:154
std::string convertReturnTypeToString(const ReturnType result)
Convert the given ReturnType enum value to its corresponding string.
Definition: BelosTypes.cpp:179
StatusType convertStringToStatusType(const std::string &status)
The StatusType enum value corresponding to the given string name.
Definition: BelosTypes.cpp:72