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.
-
BOUT_CONCAT_(A, B)#
Utility to evaluate and concatenate macro symbols Note that ## operator doesn’t evaluate symols A or B
-
BOUT_CONCAT(A, B)#
Functions
-
template<typename T>
int invert3x3(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> inline BOUT_HOST_DEVICE T SQ (const T &t)
Calculate the square of a variable
t
i.e. t * t
-
template<typename T>
T BOUTMIN(T a)# Calculate the minimum of a list of values using the a < b operator
-
inline bool is_pow2(int x)#
Check if a number is a power of 2
-
inline 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::string toString(const T &val)# Convert a value to a string by writing to a stringstream
-
inline 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.
-
inline std::string toString(const DirectionTypes &dir)#
-
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 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 – [in] The string to split (not modified by call)
delim – [in] The delimiter to split on (single char)
elems – [inout] 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 – [in] The string to split (not modified by call)
delim – [in] The delimiter to split on (single char)
-
std::string trim(const std::string &s, const std::string &c =
" \"
) Strips leading and trailing spaces from a string
- Parameters:
s – [in] The string to trim (not modified)
c – [in] Collection of characters to remove
-
std::string trimLeft(const std::string &s, const std::string &c =
" \
) Strips leading spaces from a string
- Parameters:
s – [in] The string to trim (not modified)
c – [in] Collection of characters to remove
-
std::string trimRight(const std::string &s, const std::string &c =
" \"
) Strips leading spaces from a string
- Parameters:
s – [in] The string to trim (not modified)
c – [in] Collection of characters to remove
-
std::string trimComments(const std::string &s, const std::string &c = "#;")
Strips the comments from a string
- Parameters:
s – [in] The string to trim (not modified)
c – [in] Collection of characters to remove
-
std::string::size_type editDistance(const std::string &str1, const std::string &str2)
Returns the “edit distance” between two strings: how many insertions, deletions, substitutions and transpositions are needed to transform
str1
intostr2
Implemented using the “optimal string alignment distance” from Wikipedia: https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance#Optimal_string_alignment_distance
-
template<typename T>
class Matrix# - #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() = default#
-
inline 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!
-
inline bool empty() const#
-
Matrix() = default#
-
template<typename T>
class Tensor# - #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() = default#
-
inline 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!
-
inline bool empty() const#
-
Tensor() = default#
-
namespace bout
Provides access to the Hypre library, handling initialisation and finalisation.
Usage
#include <bout/hyprelib.hxx>
class MyClass { public:
private: HypreLib lib; };
This will then automatically initialise Hypre the first time an object is created, and finalise it when the last object is destroyed.
Copyright 2012 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/.
Information about the version of BOUT++
The build system will update this file on every commit, which may result in files that include it getting rebuilt. Therefore it should be included in as few places as possible
Information about the version of BOUT++
The build system will update this file at configure-time
SNB model
-
namespace utils
Functions
-
template<class T, class ...Args>
_Unique_if<T>::_Single_object make_unique(Args&&... args)#
-
template<class T>
_Unique_if<T>::_Unknown_bound make_unique(size_t n)#
-
template<class T, class ...Args>
_Unique_if<T>::_Known_bound make_unique(Args&&...) = delete#
-
template<class Key, class Compare, class Alloc, class Pred>
std::multiset<Key, Compare, Alloc>::size_type erase_if(std::multiset<Key, Compare, Alloc> &c, Pred pred)# Erases all elements from
c
that satisfy the predicatepred
from the container. Implementation of C++20’s std::erase_if, taken from https://en.cppreference.com/w/cpp/container/multiset/erase_if CC-BY-SA
-
template<class Key, class Compare, class Alloc, class Pred>
std::set<Key, Compare, Alloc>::size_type erase_if(std::set<Key, Compare, Alloc> &c, Pred pred)# Erases all elements from
c
that satisfy the predicatepred
from the container. Implementation of C++20’s std::erase_if, taken from https://en.cppreference.com/w/cpp/container/set/erase_if CC-BY-SA
-
template<class T, class Alloc, class U>
std::vector<T, Alloc>::size_type erase(std::vector<T, Alloc> &c, const U &value)# Erases all elements from
c
that compare equal tovalue
from the container. Implementation of C++20’s std::erase_if, taken from https://en.cppreference.com/w/cpp/container/vector/erase2 CC-BY-SA
-
template<class T, class Alloc, class Pred>
std::vector<T, Alloc>::size_type erase_if(std::vector<T, Alloc> &c, Pred pred)# Erases all elements from
c
that satisfy the predicatepred
from the container. Implementation of C++20’s std::erase_if, taken from https://en.cppreference.com/w/cpp/container/vector/erase2 CC-BY-SA
-
template<class T>
struct _Unique_if#
-
template<typename T>
struct function_traits#
-
template<typename R, typename ...Args>
struct function_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
-
template<size_t i>
struct arg#
-
template<size_t i>
-
template<class T, class ...Args>
-
namespace utils