Teuchos_Utils.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00053
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
00061
00062 if( first_non_white > last_non_white )
00063 return std::string("");
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 }