Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
13namespace Teuchos {
14
15double Utils::chopVal_ = 1.0e-16;
16
17double Utils::chop(const double& x)
18{
19 if (std::fabs(x) < chopVal_) return 0;
20 return x;
21}
22
23std::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 ;
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(
43 );
44 // Above, if only whitespace is found, last_non_white==0 on termination of
45 // the loop!
47 return std::string(""); // The std::string is all whitespace!
49}
50
51std::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
58std::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
65std::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
72std::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 ) {
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;
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
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
A utilities class for 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.
Smart reference counting pointer class for automatic garbage collection.
static std::string toString(const double &x)
Write a double as a std::string.
static std::string trimWhiteSpace(const std::string &str)
Trim whitespace from beginning and end of std::string.
static std::string getParallelExtension(int procRank=-1, int numProcs=-1)
Get a parallel file name extention .
static double chop(const double &x)
Set a number to zero if it is less than getChopVal().
static bool isWhiteSpace(const char c)
Determine if a char is whitespace or not.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...