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 (fabs(x) < chopVal_) return 0;
00039   return x;
00040 }
00041 
00042 string Utils::getParallelExtension(
00043   int         procRank_in
00044   ,int        numProcs_in
00045   )
00046 {
00047 
00048   int        procRank = -1;
00049   int        numProcs = -1;
00050   if( numProcs_in > 0 ) {
00051     procRank = procRank_in;
00052     numProcs = numProcs_in;
00053   }
00054   else {
00055     procRank = Teuchos::GlobalMPISession::getRank();
00056     numProcs = Teuchos::GlobalMPISession::getNProc();
00057   }
00058 
00059   int maxProcOrder = 1;
00060   double tmp = numProcs;
00061   for( int i = 0; i < 10; ++i, tmp *= 0.1 ) {
00062     if(tmp >= 1.0)
00063       ++maxProcOrder;
00064     else
00065       break;
00066   }
00067 
00068   std::ostringstream parallelExtension;
00069   parallelExtension
00070     << std::setfill('0')
00071     << std::right << std::setw(maxProcOrder)
00072     << numProcs
00073     << "."
00074     << std::setfill('0')
00075     << std::right << std::setw(maxProcOrder)
00076     << procRank;
00077   return parallelExtension.str();
00078 }
00079 
00080 string Utils::toString(const int& x)
00081 {
00082   char s[100];
00083   sprintf(s, "%d", x);
00084   return string(s);
00085 }
00086 
00087 string Utils::toString(const unsigned int& x)
00088 {
00089   char s[100];
00090   sprintf(s, "%d", x);
00091   return string(s);
00092 }
00093 
00094 string Utils::toString(const double& x)
00095 {
00096   char s[100];
00097   sprintf(s, "%g", x);
00098   return string(s);
00099 }
00100 
00101 
00102 } // end namespace Teuchos

Generated on Thu Sep 18 12:30:31 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1