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 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_Utils.hpp"
12 
13 namespace Teuchos {
14 
15 double Utils::chopVal_ = 1.0e-16;
16 
17 double Utils::chop(const double& x)
18 {
19  if (std::fabs(x) < chopVal_) return 0;
20  return x;
21 }
22 
23 std::string Utils::trimWhiteSpace( const std::string& str )
24 {
25  typedef std::string::size_type size_type;
26  const size_type len = str.length();
27  if (len==0) {
28  return str;
29  }
30  size_type first_non_white = 0;
31  for(
32  first_non_white = 0 ;
33  isWhiteSpace(str[first_non_white]) && first_non_white < len ;
34  ++first_non_white
35  );
36  // Above, if only whitespace is found, then first_non_white==len on
37  // termination of the loop!
38  size_type last_non_white = 0;
39  for(
40  last_non_white = len-1 ;
41  isWhiteSpace(str[last_non_white]) && (last_non_white != 0);
42  --last_non_white
43  );
44  // Above, if only whitespace is found, last_non_white==0 on termination of
45  // the loop!
46  if( first_non_white > last_non_white )
47  return std::string(""); // The std::string is all whitespace!
48  return str.substr(first_non_white,last_non_white-first_non_white+1);
49 }
50 
51 std::string Utils::toString(const int& x)
52 {
53  char s[100];
54  std::snprintf(s, sizeof(s), "%d", x);
55  return std::string(s);
56 }
57 
58 std::string Utils::toString(const long long& x)
59 {
60  char s[100];
61  std::snprintf(s, sizeof(s), "%lld", x);
62  return std::string(s);
63 }
64 
65 std::string Utils::toString(const unsigned int& x)
66 {
67  char s[100];
68  std::snprintf(s, sizeof(s), "%d", x);
69  return std::string(s);
70 }
71 
72 std::string Utils::toString(const double& x)
73 {
74  char s[100];
75  std::snprintf(s, sizeof(s), "%g", x);
76  return std::string(s);
77 }
78 
80  int procRank_in
81  ,int numProcs_in
82  )
83 {
84 
85  int procRank = -1;
86  int numProcs = -1;
87  if( numProcs_in > 0 ) {
88  procRank = procRank_in;
89  numProcs = numProcs_in;
90  }
91  else {
94  }
95 
96  int maxProcOrder = 1;
97  double tmp = numProcs;
98  for( int i = 0; i < 10; ++i, tmp *= 0.1 ) {
99  if(tmp >= 1.0)
100  ++maxProcOrder;
101  else
102  break;
103  }
104 
105  std::ostringstream parallelExtension;
106  parallelExtension
107  << std::setfill('0')
108  << std::right << std::setw(maxProcOrder)
109  << numProcs
110  << "."
111  << std::setfill('0')
112  << std::right << std::setw(maxProcOrder)
113  << procRank;
114  return parallelExtension.str();
115 }
116 
117 } // 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.