Teuchos - Trilinos Tools Package
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
core
src
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
"
11
#include "
Teuchos_GlobalMPISession.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
79
std::string
Utils::getParallelExtension
(
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
{
92
procRank =
Teuchos::GlobalMPISession::getRank
();
93
numProcs =
Teuchos::GlobalMPISession::getNProc
();
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
Teuchos_GlobalMPISession.hpp
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Teuchos_Utils.hpp
A utilities class for Teuchos.
Teuchos::GlobalMPISession::getRank
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
Definition
Teuchos_GlobalMPISession.cpp:201
Teuchos::GlobalMPISession::getNProc
static int getNProc()
The number of processes in MPI_COMM_WORLD.
Definition
Teuchos_GlobalMPISession.cpp:208
Teuchos::Utils::toString
static std::string toString(const double &x)
Write a double as a std::string.
Definition
Teuchos_Utils.cpp:72
Teuchos::Utils::trimWhiteSpace
static std::string trimWhiteSpace(const std::string &str)
Trim whitespace from beginning and end of std::string.
Definition
Teuchos_Utils.cpp:23
Teuchos::Utils::getParallelExtension
static std::string getParallelExtension(int procRank=-1, int numProcs=-1)
Get a parallel file name extention .
Definition
Teuchos_Utils.cpp:79
Teuchos::Utils::chop
static double chop(const double &x)
Set a number to zero if it is less than getChopVal().
Definition
Teuchos_Utils.cpp:17
Teuchos::Utils::isWhiteSpace
static bool isWhiteSpace(const char c)
Determine if a char is whitespace or not.
Definition
Teuchos_Utils.hpp:44
Teuchos
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Generated by
1.17.0