Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Utils.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
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 "Teuchos_Utils.hpp"
44 
45 namespace Teuchos {
46 
47 double Utils::chopVal_ = 1.0e-16;
48 
49 double Utils::chop(const double& x)
50 {
51  if (std::fabs(x) < chopVal_) return 0;
52  return x;
53 }
54 
55 std::string Utils::trimWhiteSpace( const std::string& str )
56 {
57  typedef std::string::size_type size_type;
58  const size_type len = str.length();
59  if (len==0) {
60  return str;
61  }
62  size_type first_non_white = 0;
63  for(
64  first_non_white = 0 ;
65  isWhiteSpace(str[first_non_white]) && first_non_white < len ;
66  ++first_non_white
67  );
68  // Above, if only whitespace is found, then first_non_white==len on
69  // termination of the loop!
70  size_type last_non_white = 0;
71  for(
72  last_non_white = len-1 ;
73  isWhiteSpace(str[last_non_white]) && (last_non_white != 0);
74  --last_non_white
75  );
76  // Above, if only whitespace is found, last_non_white==0 on termination of
77  // the loop!
78  if( first_non_white > last_non_white )
79  return std::string(""); // The std::string is all whitespace!
80  return str.substr(first_non_white,last_non_white-first_non_white+1);
81 }
82 
83 std::string Utils::toString(const int& x)
84 {
85  char s[100];
86  std::sprintf(s, "%d", x);
87  return std::string(s);
88 }
89 
90 std::string Utils::toString(const long long& x)
91 {
92  char s[100];
93  std::sprintf(s, "%lld", x);
94  return std::string(s);
95 }
96 
97 std::string Utils::toString(const unsigned int& x)
98 {
99  char s[100];
100  std::sprintf(s, "%d", x);
101  return std::string(s);
102 }
103 
104 std::string Utils::toString(const double& x)
105 {
106  char s[100];
107  std::sprintf(s, "%g", x);
108  return std::string(s);
109 }
110 
112  int procRank_in
113  ,int numProcs_in
114  )
115 {
116 
117  int procRank = -1;
118  int numProcs = -1;
119  if( numProcs_in > 0 ) {
120  procRank = procRank_in;
121  numProcs = numProcs_in;
122  }
123  else {
126  }
127 
128  int maxProcOrder = 1;
129  double tmp = numProcs;
130  for( int i = 0; i < 10; ++i, tmp *= 0.1 ) {
131  if(tmp >= 1.0)
132  ++maxProcOrder;
133  else
134  break;
135  }
136 
137  std::ostringstream parallelExtension;
138  parallelExtension
139  << std::setfill('0')
140  << std::right << std::setw(maxProcOrder)
141  << numProcs
142  << "."
143  << std::setfill('0')
144  << std::right << std::setw(maxProcOrder)
145  << procRank;
146  return parallelExtension.str();
147 }
148 
149 } // end namespace Teuchos
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
static int getNProc()
The number of processes in MPI_COMM_WORLD.
static std::string toString(const double &x)
Write a double as a std::string.
static bool isWhiteSpace(const char c)
Determine if a char is whitespace or not.
static std::string trimWhiteSpace(const std::string &str)
Trim whitespace from beginning and end of std::string.
static double chop(const double &x)
Set a number to zero if it is less than getChopVal().
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
static std::string getParallelExtension(int procRank=-1, int numProcs=-1)
Get a parallel file name extention .
A utilities class for Teuchos.