16#ifndef dealii_utilities_h
17#define dealii_utilities_h
23#include <boost/archive/binary_iarchive.hpp>
24#include <boost/archive/binary_oarchive.hpp>
25#include <boost/core/demangle.hpp>
26#include <boost/iostreams/device/array.hpp>
27#include <boost/iostreams/device/back_inserter.hpp>
28#include <boost/iostreams/filtering_streambuf.hpp>
29#include <boost/serialization/array.hpp>
30#include <boost/serialization/complex.hpp>
31#include <boost/serialization/vector.hpp>
42#ifdef DEAL_II_WITH_ZLIB
43# include <boost/iostreams/filter/gzip.hpp>
50template <
int dim,
typename Number>
89 template <
int dim,
typename Number>
90 std::vector<std::array<std::uint64_t, dim>>
99 std::vector<std::array<std::uint64_t, dim>>
101 const std::vector<std::array<std::uint64_t, dim>> &points,
179 std::vector<unsigned char>
218 template <
typename number>
238 template <
typename Number>
261 dim_string(
const int dim,
const int spacedim);
328 std::vector<std::string>
336 std::vector<std::string>
349 std::vector<std::string>
351 const unsigned int width,
369 std::pair<int, unsigned int>
378 const std::string &
from,
379 const std::string &
to);
387 trim(
const std::string &input);
436 template <
int N,
typename T>
445 template <
typename T>
449#if defined(DEBUG) && !defined(DEAL_II_CXX14_CONSTEXPR_BUG)
452# if KOKKOS_VERSION >= 30600
457 abort_or_throw_on_exception,
462 "ExcMessage(\"The exponent must not be negative!\")",
463 ExcMessage(
"The exponent must not be negative!"));
466# ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
470 abort_or_throw_on_exception,
475 "ExcMessage(\"The exponent must not be negative!\")",
476 ExcMessage(
"The exponent must not be negative!"));
501 static_assert(std::is_integral<T>::value,
"Only integral types supported");
506 (((
iexp % 2 == 1) ? base : 1) *
531 template <
typename Iterator,
typename T>
541 template <
typename Iterator,
typename T,
typename Comp>
550 template <
typename Integer>
559 template <
typename Integer>
617 template <
typename T>
619 pack(
const T &
object,
631 template <
typename T>
666 template <
typename T>
678 template <
typename T>
680 unpack(
const std::vector<char>::const_iterator &cbegin,
681 const std::vector<char>::const_iterator &cend,
716 template <
typename T,
int N>
730 template <
typename T,
int N>
732 unpack(
const std::vector<char>::const_iterator &cbegin,
733 const std::vector<char>::const_iterator &cend,
741 get_bit(
const unsigned char number,
const unsigned int n);
748 set_bit(
unsigned char &number,
const unsigned int n,
const bool x);
808 template <
typename To,
typename From>
815 template <
typename T>
822 template <
typename T>
829 template <
typename T>
836 template <
typename T>
843 template <
typename T>
981 template <
int N,
typename T>
985 Assert(((std::is_integral<T>::value ==
true) && (N >= 0)) ||
986 (std::is_integral<T>::value ==
false),
987 ExcMessage(
"If the type of the argument, T, is an integer type, "
988 "then the exponent N must be a non-negative integer "
989 "because the result would otherwise not be an integer."));
1009 return boost::core::demangle(
typeid(t).name());
1014 template <
typename Iterator,
typename T>
1023 template <
typename Iterator,
typename T,
typename Comp>
1032 "The given iterators do not satisfy the proper ordering."));
1034 unsigned int len =
static_cast<unsigned int>(last -
first);
1100 const unsigned int half =
len >> 1;
1128 template <
typename T>
1136 template <
typename T>
1140 std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value;
1145 template <
typename T>
1149 std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value;
1162 template <
typename T>
1165 std::vector<char> &)
1173 template <
typename T,
1174 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1175 std::is_trivially_copyable<T>::value>>
1178 const std::vector<T> &
object,
1191 reinterpret_cast<const char *
>(&
vector_size + 1));
1196 reinterpret_cast<const char *
>(
object.data()),
1197 reinterpret_cast<const char *
>(
object.data() +
1203 template <
typename T,
1204 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1205 std::is_trivially_copyable<T>::value>>
1208 const std::vector<std::vector<T>> &
object,
1211 using size_type =
typename std::vector<T>::size_type;
1215 std::vector<size_type> sizes;
1217 for (
const auto &a : object)
1220 sizes.push_back(a.size());
1232 reinterpret_cast<const char *
>(&
vector_size + 1));
1237 reinterpret_cast<const char *
>(sizes.data()),
1238 reinterpret_cast<const char *
>(sizes.data() +
1242 for (
const auto &a : object)
1244 reinterpret_cast<const char *
>(a.data()),
1245 reinterpret_cast<const char *
>(a.data() + a.size()));
1250 template <
typename T>
1253 const std::vector<char>::const_iterator &,
1254 const std::vector<char>::const_iterator &,
1263 template <
typename T,
1264 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1265 std::is_trivially_copyable<T>::value>>
1268 const std::vector<char>::const_iterator &cbegin,
1269 const std::vector<char>::const_iterator &cend,
1270 std::vector<T> &
object)
1279 Assert(
static_cast<std::ptrdiff_t
>(cend - cbegin) ==
1282 ExcMessage(
"The given buffer has the wrong size."));
1290 reinterpret_cast<const T *
>(&*cbegin +
sizeof(
vector_size));
1298 template <
typename T,
1299 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1300 std::is_trivially_copyable<T>::value>>
1303 const std::vector<char>::const_iterator &cbegin,
1304 const std::vector<char>::const_iterator &cend,
1305 std::vector<std::vector<T>> &
object)
1308 using size_type =
typename std::vector<T>::size_type;
1309 std::vector<char>::const_iterator iterator = cbegin;
1322 for (
const auto a : sizes)
1325 Assert(
static_cast<std::ptrdiff_t
>(cend - iterator) ==
1327 ExcMessage(
"The given buffer has the wrong size."));
1334 object[i].insert(
object[i].end(),
1335 reinterpret_cast<const T *
>(&*iterator),
1336 reinterpret_cast<const T *
>(&*iterator +
1337 sizeof(T) * sizes[i]));
1338 iterator +=
sizeof(T) * sizes[i];
1342 ExcMessage(
"The given buffer has the wrong size."));
1349 template <
typename T>
1355 std::size_t size = 0;
1370 size = (std::is_same<T, std::tuple<>>::value ? 0 :
sizeof(T));
1404 boost::iostreams::filtering_ostreambuf
fosb;
1405#ifdef DEAL_II_WITH_ZLIB
1407 fosb.push(boost::iostreams::gzip_compressor());
1413 boost::archive::binary_oarchive
boa(
fosb);
1425 template <
typename T>
1429 std::vector<char>
buffer;
1436 template <
typename T>
1438 unpack(
const std::vector<char>::const_iterator &cbegin,
1439 const std::vector<char>::const_iterator &cend,
1454 const std::size_t size =
1455 (std::is_same<T, std::tuple<>>::value ? 0 :
sizeof(T));
1463 std::memcpy(&
object, &*cbegin, size);
1488 boost::iostreams::filtering_istreambuf
fisb;
1489#ifdef DEAL_II_WITH_ZLIB
1491 fisb.push(boost::iostreams::gzip_decompressor());
1495 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1497 boost::archive::binary_iarchive
bia(
fisb);
1508 template <
typename T>
1516 template <
typename T,
int N>
1518 unpack(
const std::vector<char>::const_iterator &cbegin,
1519 const std::vector<char>::const_iterator &cend,
1527 sizeof(T) * N < 256)
1529 Assert(std::distance(cbegin, cend) ==
sizeof(T) * N,
1536 boost::iostreams::filtering_istreambuf
fisb;
1537#ifdef DEAL_II_WITH_ZLIB
1539 fisb.push(boost::iostreams::gzip_decompressor());
1543 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1545 boost::archive::binary_iarchive
bia(
fisb);
1551 template <
typename T,
int N>
1566 get_bit(
const unsigned char number,
const unsigned int n)
1573 return ((number >> n) & 1U) != 0
u;
1579 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1586 number ^= (-
static_cast<unsigned char>(
x) ^ number) & (1U << n);
1591 template <
typename To,
typename From>
1592 inline std::unique_ptr<To>
1598 if (
To *
cast =
dynamic_cast<To *
>(p.get()))
1605 throw std::bad_cast();
1610 template <
typename T>
1619 template <
typename T>
1628 template <
typename T>
1637 template <
typename T>
1646 template <
typename T>
1655 template <
typename Integer>
1656 std::vector<Integer>
1659 const std::size_t n = permutation.size();
1661 std::vector<Integer> out(n);
1662 for (std::size_t i = 0; i < n; ++i)
1663 out[i] = n - 1 - permutation[i];
1670 template <
typename Integer>
1671 std::vector<Integer>
1674 const std::size_t n = permutation.size();
1678 for (std::size_t i = 0; i < n; ++i)
1681 out[permutation[i]] = i;
1686 for (std::size_t i = 0; i < n; ++i)
1688 ExcMessage(
"The given input permutation had duplicate entries!"));
1709 serialize(
Archive &
ar, std::tuple<Args...> &t,
const unsigned int version)
1711 ar &std::get<N - 1>(t);
1721 serialize(
Archive &
ar, std::tuple<Args...> &t,
const unsigned int version)
1731 serialize(
Archive &
ar, std::tuple<Args...> &t,
const unsigned int version)
1733 Serialize<
sizeof...(Args)>::serialize(
ar, t, version);
const_iterator cend() const
const_iterator cbegin() const
value_type * data() const noexcept
#define DEAL_II_CONSTEXPR_IN_CONDITIONAL
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_FALLTHROUGH
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
void get_memory_stats(MemoryStats &stats)
std::string get_hostname()
void posix_memalign(void **memptr, std::size_t alignment, std::size_t size)
const std::string get_current_vectorization_level()
void create_vector_of_trivially_copyable_from_buffer(const std::vector< char >::const_iterator &, const std::vector< char >::const_iterator &, T &)
void append_vector_of_trivially_copyable_to_buffer(const T &, std::vector< char > &)
constexpr T pow(const T base, const int iexp)
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
T & get_underlying_value(T &p)
Number truncate_to_n_digits(const Number number, const unsigned int n_digits)
std::string type_to_string(const T &t)
size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
std::string dim_string(const int dim, const int spacedim)
std::uint64_t pack_integers(const std::array< std::uint64_t, dim > &index, const int bits_per_dim)
bool get_bit(const unsigned char number, const unsigned int n)
std::pair< int, unsigned int > get_integer_at_position(const std::string &name, const unsigned int position)
std::string encode_base64(const std::vector< unsigned char > &binary_input)
std::unique_ptr< To > dynamic_unique_cast(std::unique_ptr< From > &&p)
std::string replace_in_string(const std::string &input, const std::string &from, const std::string &to)
std::vector< unsigned char > decode_base64(const std::string &base64_input)
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter=' ')
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
std::vector< std::array< std::uint64_t, dim > > inverse_Hilbert_space_filling_curve(const std::vector< Point< dim, Number > > &points, const int bits_per_dim=64)
std::string compress(const std::string &input)
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
bool match_at_string_start(const std::string &name, const std::string &pattern)
T unpack(const std::vector< char > &buffer, const bool allow_compression=true)
std::string decompress(const std::string &compressed_input)
unsigned int needed_digits(const unsigned int max_number)
Iterator lower_bound(Iterator first, Iterator last, const T &val)
double string_to_double(const std::string &s)
std::string dealii_version_string()
void set_bit(unsigned char &number, const unsigned int n, const bool x)
std::string trim(const std::string &input)
double generate_normal_random_number(const double a, const double sigma)
std::vector< Integer > reverse_permutation(const std::vector< Integer > &permutation)
std::vector< Integer > invert_permutation(const std::vector< Integer > &permutation)
int string_to_int(const std::string &s)
void issue_error_noreturn(ExceptionHandling handling, const char *file, int line, const char *function, const char *cond, const char *exc_name, ExceptionType e)
static const unsigned int invalid_unsigned_int
#define DEAL_II_HOST_DEVICE
static constexpr bool value