Belos Version of the Day
BelosTypes.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 //
00004 //                 Belos: Block Linear Solvers Package
00005 //                  Copyright 2004 Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 //@HEADER
00041 
00042 #include <BelosTypes.hpp>
00043 
00044 namespace Belos {
00045 
00046   namespace {
00047     const char*
00048     convertStatusTypeToRawString (const StatusType status)
00049     {
00050       if (status == Passed) {
00051         return "Passed";
00052       } else if (status == Failed) {
00053         return "Failed";
00054       } else if (status == Undefined) {
00055         return "Undefined";
00056       } else {
00057         TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
00058           "Belos::convertStatusTypeToRawString: Invalid StatusType enum value "
00059           << status << ".");
00060       }
00061     }
00062   } // namespace (anonymous)
00063 
00064   const char* TEUCHOS_DEPRECATED
00065   toString (const StatusType status)
00066   {
00067     return convertStatusTypeToRawString (status);
00068   }
00069 
00070   std::string
00071   convertStatusTypeToString (const StatusType status)
00072   {
00073     return convertStatusTypeToRawString (status);
00074   }
00075 
00076   StatusType
00077   convertStringToStatusType (const std::string& status)
00078   {
00079     if (status == "Passed") {
00080       return Passed;
00081     } else if (status == "Failed") {
00082       return Failed;
00083     } else if (status == "Undefined") {
00084       return Undefined;
00085     } else {
00086       TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
00087         "Belos::convertStringToStatusType: Invalid string \"" << status
00088         << "\".");
00089     }
00090   }
00091 
00092   ScaleType
00093   convertStringToScaleType (const std::string& scaleType)
00094   {
00095     if (scaleType == "Norm of Initial Residual") {
00096       return Belos::NormOfInitRes;
00097     } else if (scaleType == "Norm of Preconditioned Initial Residual") {
00098       return Belos::NormOfPrecInitRes;
00099     } else if (scaleType == "Norm of RHS") {
00100        return Belos::NormOfRHS;
00101     } else if (scaleType == "None") {
00102       return Belos::None;
00103     } else if (scaleType == "User Provided") {
00104       return Belos::UserProvided;
00105     } else {
00106       TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
00107         "Belos::convertStringToScaleType(): Invalid residual scaling type \""
00108         << scaleType << "\".");
00109     }
00110   }
00111 
00112   std::string
00113   convertScaleTypeToString (const ScaleType scaleType)
00114   {
00115     if (scaleType == Belos::NormOfInitRes) {
00116       return "Norm of Initial Residual";
00117     } else if (scaleType == Belos::NormOfPrecInitRes) {
00118       return "Norm of Preconditioned Initial Residual";
00119     } else if (scaleType == Belos::NormOfRHS) {
00120       return "Norm of RHS";
00121     } else if (scaleType == Belos::None) {
00122       return "None";
00123     } else if (scaleType == Belos::UserProvided) {
00124       return "User Provided";
00125     } else {
00126       TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
00127         "Belos::convertScaleTypeToString(): Invalid residual scaling type "
00128         "value " << scaleType << ".");
00129     }
00130   }
00131 
00132   std::string
00133   convertMsgTypeToString (const MsgType msgType)
00134   {
00135     typedef std::vector<int>::size_type size_type;
00136 
00137     // Wouldn't it be nice if C++ enums had introspection and could
00138     // be enumerated?
00139     const size_type numValidTypes = 8;
00140     const int validTypes[] = {
00141       Belos::Errors,
00142       Belos::Warnings,
00143       Belos::IterationDetails,
00144       Belos::OrthoDetails,
00145       Belos::FinalSummary,
00146       Belos::TimingDetails,
00147       Belos::StatusTestDetails,
00148       Belos::Debug
00149     };
00150     const char* typeNames[] = {
00151       "Errors",
00152       "Warnings",
00153       "IterationDetails",
00154       "OrthoDetails",
00155       "FinalSummary",
00156       "TimingDetails",
00157       "StatusTestDetails",
00158       "Debug"
00159     };
00160 
00161     // We first generate a list, and only then build a single string.
00162     // This helps us decide where to put the commas.  The list just
00163     // uses the indices of the valid names, rather than the valid
00164     // names themselves, in order to save space and time.  We use
00165     // size_type for the indices to avoid signed/unsigned comparisons.
00166     std::vector<size_type> theList;
00167     for (size_type nameIndex = 0; nameIndex < numValidTypes; ++nameIndex) {
00168       if (msgType & validTypes[nameIndex]) {
00169         theList.push_back (nameIndex);
00170       }
00171     }
00172     std::ostringstream os;
00173     for (size_type k = 0; k < theList.size(); ++k) {
00174       const size_type nameIndex = theList[k];
00175       os << typeNames[nameIndex];
00176       if (nameIndex < theList.size() - 1) {
00177         os << ",";
00178       }
00179     }
00180     return os.str();
00181   }
00182 
00183   std::string
00184   convertReturnTypeToString (const ReturnType result)
00185   {
00186     if (result == Belos::Converged) {
00187       return "Converged";
00188     } else if (result == Belos::Unconverged) {
00189       return "Unconverged";
00190     } else {
00191       TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
00192         "Belos::convertReturnTypeToString: Invalid ReturnType enum value "
00193         << result << ".");
00194     }
00195   }
00196 
00197 } // end Belos namespace
00198 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines