Teuchos_Utils.cpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_Utils.hpp"
00030 #include "Teuchos_GlobalMPISession.hpp"
00031 
00032 namespace Teuchos {
00033 
00034 double Utils::chopVal_ = 1.0e-16;
00035 
00036 double Utils::chop(const double& x) 
00037 {
00038   if (std::fabs(x) < chopVal_) return 0;
00039   return x;
00040 }
00041 
00042 std::string Utils::trimWhiteSpace( const std::string& str )
00043 {
00044   typedef std::string::size_type size_type;
00045   const size_type len = str.length();
00046   size_type first_non_white = 0;
00047   for(
00048     first_non_white = 0 ;
00049     isWhiteSpace(str[first_non_white]) && first_non_white < len ;
00050     ++first_non_white
00051     );
00052   // Above, if only whitespace is found, then first_non_white==len on
00053   // termination of the loop!
00054   size_type last_non_white = 0;
00055   for(
00056     last_non_white = len-1 ;
00057     isWhiteSpace(str[last_non_white]) && last_non_white >= 0;
00058     --last_non_white
00059     );
00060   // Above, if only whitespace is found, last_non_white==0 on termination of
00061   // the loop!
00062   if( first_non_white > last_non_white )
00063     return std::string(""); // The std::string is all whitespace!
00064   return str.substr(first_non_white,last_non_white-first_non_white+1);
00065 }
00066 
00067 std::string Utils::toString(const int& x)
00068 {
00069   char s[100];
00070   std::sprintf(s, "%d", x);
00071   return std::string(s);
00072 }
00073 
00074 std::string Utils::toString(const unsigned int& x)
00075 {
00076   char s[100];
00077   std::sprintf(s, "%d", x);
00078   return std::string(s);
00079 }
00080 
00081 std::string Utils::toString(const double& x)
00082 {
00083   char s[100];
00084   std::sprintf(s, "%g", x);
00085   return std::string(s);
00086 }
00087 
00088 std::string Utils::getParallelExtension(
00089   int         procRank_in
00090   ,int        numProcs_in
00091   )
00092 {
00093 
00094   int        procRank = -1;
00095   int        numProcs = -1;
00096   if( numProcs_in > 0 ) {
00097     procRank = procRank_in;
00098     numProcs = numProcs_in;
00099   }
00100   else {
00101     procRank = Teuchos::GlobalMPISession::getRank();
00102     numProcs = Teuchos::GlobalMPISession::getNProc();
00103   }
00104 
00105   int maxProcOrder = 1;
00106   double tmp = numProcs;
00107   for( int i = 0; i < 10; ++i, tmp *= 0.1 ) {
00108     if(tmp >= 1.0)
00109       ++maxProcOrder;
00110     else
00111       break;
00112   }
00113 
00114   std::ostringstream parallelExtension;
00115   parallelExtension
00116     << std::setfill('0')
00117     << std::right << std::setw(maxProcOrder)
00118     << numProcs
00119     << "."
00120     << std::setfill('0')
00121     << std::right << std::setw(maxProcOrder)
00122     << procRank;
00123   return parallelExtension.str();
00124 }
00125 
00126 } // end namespace Teuchos

Generated on Wed May 12 21:40:33 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7