File utils.hxx¶
A mix of short utilities for memory management, strings, and some simple but common calculations
Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu
Contact: Ben Dudson, bd512@york.ac.uk
This file is part of BOUT++.
BOUT++ is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
BOUT++ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with BOUT++. If not, see http://www.gnu.org/licenses/.
Defines
-
bout_vsnprintf
(buf, len, fmt)¶ the bout_vsnprintf macro: The first argument is an char * buffer of length len. It needs to have been allocated with new[], as it may be reallocated. len: the length of said buffer. May be changed, mussn’t be const. fmt: the const char * descriping the format. note that fmt should be the first argument of the function of type const char * and has to be directly followed by the variable arguments.
Functions
-
template<typename
T
>
intinvert3x3
(Matrix<T> &a, BoutReal small = 1.0e-15)¶ Explicit inversion of a 3x3 matrix
a
The input
small
determines how small the determinant must be for us to throw due to the matrix being singular (ill conditioned); If small is less than zero then instead of throwing we return 1. This is ugly but can be used to support some use cases.
-
template<typename
T
>
TBOUTMIN
(T a)¶ Calculate the minimum of a list of values using the a < b operator
-
bool
is_pow2
(int x)¶ Check if a number is a power of 2
-
BoutReal
MINMOD
(BoutReal a, BoutReal b)¶ The minimum absolute value of
a
andb
if
a
andb
have opposite signs, return zeroif |a| < |b| then return a, otherwise return b
-
char *
copy_string
(const char *s) Allocate memory and copy string
s
-
template<class
T
>
std::stringtoString
(const T &val)¶ Convert a value to a string by writing to a stringstream
-
std::string
toString
(const std::string &val)¶ Simple case where input is already a string This is so that toString can be used in templates where the type may be std::string.
-
std::string
toString
(const bool &val)¶ Convert a bool to “true” or “false”.
-
std::string
toString
(const time_t &time) Convert a time stamp to a string This uses std::localtime and std::put_time
-
const std::string
lowercase
(const std::string &str) Convert a string to lower case
-
const std::string
uppercase
(const std::string &str) Convert a string to upper case
-
const std::string
lowercasequote
(const std::string &str) Convert to lower case, except inside quotes (” or ‘)
-
BoutReal
stringToReal
(const std::string &s) Convert a string to a BoutReal Throws BoutException if can’t be done
-
int
stringToInt
(const std::string &s) Convert a string to an int
Throws BoutException if can’t be done
-
std::list<std::string> &
strsplit
(const std::string &s, char delim, std::list<std::string> &elems) Split a string on a given delimiter
- Parameters
s
: The string to split (not modified by call)delim
: The delimiter to split on (single char)elems
: A list to which the pieces will be appended using push_back
-
std::list<std::string>
strsplit
(const std::string &s, char delim) Split a string on a given delimiter
- Parameters
s
: The string to split (not modified by call)delim
: The delimiter to split on (single char)
-
std::string
trim
(const std::string &s) Strips leading and trailing spaces from a string
- Parameters
s
: The string to trim (not modified)c
: Collection of characters to remove
-
std::string
trimLeft
(const std::string &s) Strips leading spaces from a string
- Parameters
s
: The string to trim (not modified)c
: Collection of characters to remove
-
std::string
trimRight
(const std::string &s) Strips leading spaces from a string
- Parameters
s
: The string to trim (not modified)c
: Collection of characters to remove
-
std::string
trimComments
(const std::string &s, const std::string &c = "#;") Strips the comments from a string
- Parameters
s
: The string to trim (not modified)c
: Collection of characters to remove
-
template<typename
T
>
classMatrix
¶ - #include <utils.hxx>
Helper class for 2D arrays
Allows bounds checking through
operator()
with CHECK > 1If either
n1
orn2
are 0, the Matrix is empty and should not be indexedPublic Functions
-
Matrix
()¶
-
Matrix
(size_type n1, size_type n2)¶
-
void
reallocate
(size_type new_size_1, size_type new_size_2)¶ Reallocate the Matrix to shape
new_size_1
bynew_size_2
Note that this invalidates the existing data!
-
Matrix &
operator=
(const Matrix &other)¶
-
T &
operator()
(size_type i1, size_type i2)¶
-
const T &
operator()
(size_type i1, size_type i2) const¶
-
Matrix &
operator=
(const T &val)¶
-
T *
begin
()¶
-
const T *
begin
() const¶
-
T *
end
()¶
-
const T *
end
() const¶
-
std::tuple<size_type, size_type>
shape
() const¶
-
bool
empty
() const¶
-
void
ensureUnique
()¶ Ensures that this Matrix does not share data with another This should be called before performing any write operations on the data.
-
Array<T> &
getData
()¶ Access the underlying storage.
-
const Array<T> &
getData
() const¶
-
-
template<typename
T
>
classTensor
¶ - #include <utils.hxx>
Helper class for 3D arrays
Allows bounds checking through
operator()
with CHECK > 1If any of
n1
,n2
orn3
are 0, the Tensor is empty and should not be indexedPublic Functions
-
Tensor
()¶
-
Tensor
(size_type n1, size_type n2, size_type n3)¶
-
void
reallocate
(size_type new_size_1, size_type new_size_2, size_type new_size_3)¶ Reallocate the Tensor with shape
new_size_1
bynew_size_2
bynew_size_3
Note that this invalidates the existing data!
-
Tensor &
operator=
(const Tensor &other)¶
-
T &
operator()
(size_type i1, size_type i2, size_type i3)¶
-
const T &
operator()
(size_type i1, size_type i2, size_type i3) const¶
-
Tensor &
operator=
(const T &val)¶
-
T *
begin
()¶
-
const T *
begin
() const¶
-
T *
end
()¶
-
const T *
end
() const¶
-
std::tuple<size_type, size_type, size_type>
shape
() const¶
-
bool
empty
() const¶
-
void
ensureUnique
()¶ Ensures that this Tensor does not share data with another This should be called before performing any write operations on the data.
-
Array<T> &
getData
()¶ Access the underlying storage.
-
const Array<T> &
getData
() const¶
-
-
namespace
bout
SNB model
-
namespace
utils
Functions
-
template<class
T
, class ...Args
>
_Unique_if<T>::_Single_objectmake_unique
(Args&&... args)¶
-
template<class
T
>
struct_Unique_if
¶ Public Types
-
template<>
using_Single_object
= std::unique_ptr<T>¶
-
template<>
-
template<class
T
>
struct_Unique_if
<T[]>¶ Public Types
-
template<>
using_Unknown_bound
= std::unique_ptr<T[]>¶
-
template<>
-
template<class
T
, size_tN
>
struct_Unique_if
<T[N]>¶ Public Types
-
template<>
using_Known_bound
= void¶
-
template<>
-
template<typename
R
, typename ...Args
>
structfunction_traits
<R (*)(Args...)>¶ - #include <utils.hxx>
Traits class to get the types of function arguments for function pointers
Use like: using some_function = int(*)(int, double, std::string); // Get the type of the first argument: using first_argument_type = bout::utils::function_traits<some_function>::arg<1>::type; // The following prints “true”: std::cout << std::boolalpha << std::is_same<double, first_argument_type>::value;
Adapted from https://stackoverflow.com/a/9065203/2043465
Public Static Attributes
-
constexpr size_t
nargs
= sizeof...(Args)¶ Total number of arguments.
-
constexpr size_t
-
template<class
-
namespace