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>
37 #include <type_traits>
42 #ifdef DEAL_II_WITH_ZLIB
43 # include <boost/iostreams/filter/gzip.hpp>
50 template <
int dim,
typename Number>
89 template <
int dim,
typename Number>
90 std::vector<std::array<std::uint64_t, dim>>
93 const int bits_per_dim = 64);
99 std::vector<std::array<std::uint64_t, dim>>
101 const std::vector<std::array<std::uint64_t, dim>> &points,
102 const int bits_per_dim = 64);
122 const int bits_per_dim);
153 decompress(
const std::string &compressed_input);
169 encode_base64(
const std::vector<unsigned char> &binary_input);
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,
352 const char delimiter =
' ');
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>
447 pow(
const T base,
const int iexp)
449 #if defined(DEBUG) && !defined(DEAL_II_CXX14_CONSTEXPR_BUG)
455 abort_or_throw_on_exception,
460 "ExcMessage(\"The exponent must not be negative!\")",
461 ExcMessage(
"The exponent must not be negative!"));
484 static_assert(std::is_integral<T>::value,
"Only integral types supported");
489 (((iexp % 2 == 1) ? base : 1) *
514 template <
typename Iterator,
typename T>
524 template <
typename Iterator,
typename T,
typename Comp>
533 template <
typename Integer>
542 template <
typename Integer>
600 template <
typename T>
602 pack(
const T &
object,
603 std::vector<char> &dest_buffer,
604 const bool allow_compression =
true);
614 template <
typename T>
616 pack(
const T &
object,
const bool allow_compression =
true);
649 template <
typename T>
651 unpack(
const std::vector<char> &buffer,
const bool allow_compression =
true);
661 template <
typename T>
663 unpack(
const std::vector<char>::const_iterator &cbegin,
664 const std::vector<char>::const_iterator &cend,
665 const bool allow_compression =
true);
699 template <
typename T,
int N>
701 unpack(
const std::vector<char> &buffer,
702 T (&unpacked_object)[
N],
703 const bool allow_compression =
true);
713 template <
typename T,
int N>
715 unpack(
const std::vector<char>::const_iterator &cbegin,
716 const std::vector<char>::const_iterator &cend,
717 T (&unpacked_object)[
N],
718 const bool allow_compression =
true);
724 get_bit(
const unsigned char number,
const unsigned int n);
731 set_bit(
unsigned char &number,
const unsigned int n,
const bool x);
791 template <
typename To,
typename From>
798 template <
typename T>
805 template <
typename T>
812 template <
typename T>
819 template <
typename T>
826 template <
typename T>
955 posix_memalign(
void **memptr, std::size_t alignment, std::size_t size);
964 template <
int N,
typename T>
968 Assert(((std::is_integral<T>::value ==
true) && (
N >= 0)) ||
969 (std::is_integral<T>::value ==
false),
970 ExcMessage(
"If the type of the argument, T, is an integer type, "
971 "then the exponent N must be a non-negative integer "
972 "because the result would otherwise not be an integer."));
983 fixed_power<N / 2>(x * x));
992 return boost::core::demangle(
typeid(t).name());
997 template <
typename Iterator,
typename T>
1006 template <
typename Iterator,
typename T,
typename Comp>
1015 "The given iterators do not satisfy the proper ordering."));
1017 unsigned int len =
static_cast<unsigned int>(last -
first);
1034 if (!comp(*
first, val))
1039 if (!comp(*
first, val))
1044 if (!comp(*
first, val))
1049 if (!comp(*
first, val))
1054 if (!comp(*
first, val))
1059 if (!comp(*
first, val))
1064 if (!comp(*
first, val))
1083 const unsigned int half = len >> 1;
1084 const Iterator middle =
first + half;
1091 if (comp(*middle, val))
1111 template <
typename T>
1119 template <
typename T>
1123 std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value;
1128 template <
typename T>
1132 std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value;
1145 template <
typename T>
1148 std::vector<char> &)
1156 template <
typename T,
1157 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1158 std::is_trivially_copyable<T>::value>>
1161 const std::vector<T> &
object,
1162 std::vector<char> & dest_buffer)
1168 dest_buffer.reserve(dest_buffer.size() +
sizeof(vector_size) +
1169 vector_size *
sizeof(
T));
1172 dest_buffer.insert(dest_buffer.end(),
1173 reinterpret_cast<const char *
>(&vector_size),
1174 reinterpret_cast<const char *
>(&vector_size + 1));
1177 if (vector_size > 0)
1178 dest_buffer.insert(dest_buffer.end(),
1179 reinterpret_cast<const char *
>(
object.data()),
1180 reinterpret_cast<const char *
>(
object.data() +
1186 template <
typename T,
1187 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1188 std::is_trivially_copyable<T>::value>>
1191 const std::vector<std::vector<T>> &
object,
1192 std::vector<char> & dest_buffer)
1195 const size_type vector_size =
object.size();
1198 std::vector<size_type> sizes;
1199 sizes.reserve(vector_size);
1200 for (
const auto &a :
object)
1202 aggregated_size += a.size();
1203 sizes.push_back(a.size());
1208 dest_buffer.reserve(dest_buffer.size() +
1209 sizeof(vector_size) * (1 + vector_size) +
1210 aggregated_size *
sizeof(
T));
1213 dest_buffer.insert(dest_buffer.end(),
1214 reinterpret_cast<const char *
>(&vector_size),
1215 reinterpret_cast<const char *
>(&vector_size + 1));
1218 if (vector_size > 0)
1219 dest_buffer.insert(dest_buffer.end(),
1220 reinterpret_cast<const char *
>(sizes.data()),
1221 reinterpret_cast<const char *
>(sizes.data() +
1225 for (
const auto &a :
object)
1226 dest_buffer.insert(dest_buffer.end(),
1227 reinterpret_cast<const char *
>(a.data()),
1228 reinterpret_cast<const char *
>(a.data() + a.size()));
1233 template <
typename T>
1236 const std::vector<char>::const_iterator &,
1237 const std::vector<char>::const_iterator &,
1246 template <
typename T,
1247 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1248 std::is_trivially_copyable<T>::value>>
1251 const std::vector<char>::const_iterator &cbegin,
1252 const std::vector<char>::const_iterator &cend,
1253 std::vector<T> &
object)
1260 memcpy(&vector_size, &*cbegin,
sizeof(vector_size));
1262 Assert(
static_cast<std::ptrdiff_t
>(cend - cbegin) ==
1263 static_cast<std::ptrdiff_t
>(
sizeof(vector_size) +
1264 vector_size *
sizeof(
T)),
1265 ExcMessage(
"The given buffer has the wrong size."));
1270 if (vector_size > 0)
1272 const auto buffer_data_begin =
1273 reinterpret_cast<const T *
>(&*cbegin +
sizeof(vector_size));
1274 const auto buffer_data_end = buffer_data_begin + vector_size;
1275 object.insert(
object.
end(), buffer_data_begin, buffer_data_end);
1281 template <
typename T,
1282 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1283 std::is_trivially_copyable<T>::value>>
1286 const std::vector<char>::const_iterator &cbegin,
1287 const std::vector<char>::const_iterator &cend,
1288 std::vector<std::vector<T>> &
object)
1292 std::vector<char>::const_iterator iterator = cbegin;
1294 memcpy(&vector_size, &*iterator,
sizeof(vector_size));
1296 object.resize(vector_size);
1297 std::vector<size_type> sizes(vector_size);
1298 if (vector_size > 0)
1299 memcpy(sizes.data(),
1300 &*iterator +
sizeof(vector_size),
1303 iterator +=
sizeof(vector_size) * (1 + vector_size);
1305 for (
const auto a : sizes)
1306 aggregated_size += a;
1308 Assert(
static_cast<std::ptrdiff_t
>(cend - iterator) ==
1309 static_cast<std::ptrdiff_t
>(aggregated_size *
sizeof(
T)),
1310 ExcMessage(
"The given buffer has the wrong size."));
1314 for (
unsigned int i = 0; i < vector_size; ++i)
1317 object[i].insert(
object[i].
end(),
1318 reinterpret_cast<const T *
>(&*iterator),
1319 reinterpret_cast<const T *
>(&*iterator +
1320 sizeof(
T) * sizes[i]));
1321 iterator +=
sizeof(
T) * sizes[i];
1325 ExcMessage(
"The given buffer has the wrong size."));
1332 template <
typename T>
1335 std::vector<char> &dest_buffer,
1336 const bool allow_compression)
1338 std::size_t size = 0;
1353 size = (std::is_same<T, std::tuple<>>::value ? 0 :
sizeof(
T));
1355 (void)allow_compression;
1356 const std::size_t previous_size = dest_buffer.size();
1357 dest_buffer.resize(previous_size + size);
1360 std::memcpy(dest_buffer.data() + previous_size, &
object, size);
1368 (allow_compression ==
false))
1370 const std::size_t previous_size = dest_buffer.size();
1379 size = dest_buffer.size() - previous_size;
1385 const std::size_t previous_size = dest_buffer.size();
1387 boost::iostreams::filtering_ostreambuf fosb;
1388 #ifdef DEAL_II_WITH_ZLIB
1389 if (allow_compression)
1390 fosb.push(boost::iostreams::gzip_compressor());
1392 (void)allow_compression;
1394 fosb.push(boost::iostreams::back_inserter(dest_buffer));
1396 boost::archive::binary_oarchive boa(fosb);
1401 size = dest_buffer.size() - previous_size;
1408 template <
typename T>
1410 pack(
const T &
object,
const bool allow_compression)
1412 std::vector<char> buffer;
1413 pack<T>(
object, buffer, allow_compression);
1419 template <
typename T>
1421 unpack(
const std::vector<char>::const_iterator &cbegin,
1422 const std::vector<char>::const_iterator &cend,
1423 const bool allow_compression)
1437 const std::size_t size =
1438 (std::is_same<T, std::tuple<>>::value ? 0 :
sizeof(
T));
1442 (void)allow_compression;
1446 std::memcpy(&
object, &*cbegin, size);
1456 (allow_compression ==
false))
1471 boost::iostreams::filtering_istreambuf fisb;
1472 #ifdef DEAL_II_WITH_ZLIB
1473 if (allow_compression)
1474 fisb.push(boost::iostreams::gzip_decompressor());
1476 (void)allow_compression;
1478 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1480 boost::archive::binary_iarchive bia(fisb);
1491 template <
typename T>
1493 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1495 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1499 template <
typename T,
int N>
1501 unpack(
const std::vector<char>::const_iterator &cbegin,
1502 const std::vector<char>::const_iterator &cend,
1503 T (&unpacked_object)[
N],
1504 const bool allow_compression)
1510 sizeof(
T) *
N < 256)
1512 Assert(std::distance(cbegin, cend) ==
sizeof(
T) *
N,
1514 std::memcpy(unpacked_object, &*cbegin,
sizeof(
T) *
N);
1519 boost::iostreams::filtering_istreambuf fisb;
1520 #ifdef DEAL_II_WITH_ZLIB
1521 if (allow_compression)
1522 fisb.push(boost::iostreams::gzip_decompressor());
1524 (void)allow_compression;
1526 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1528 boost::archive::binary_iarchive bia(fisb);
1529 bia >> unpacked_object;
1534 template <
typename T,
int N>
1537 T (&unpacked_object)[
N],
1538 const bool allow_compression)
1540 unpack<T, N>(buffer.cbegin(),
1549 get_bit(
const unsigned char number,
const unsigned int n)
1556 return ((number >> n) & 1U) != 0u;
1562 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1569 number ^= (-
static_cast<unsigned char>(x) ^ number) & (1U << n);
1574 template <
typename To,
typename From>
1575 inline std::unique_ptr<To>
1581 if (To *cast =
dynamic_cast<To *
>(p.get()))
1583 std::unique_ptr<To> result(cast);
1588 throw std::bad_cast();
1593 template <
typename T>
1602 template <
typename T>
1611 template <
typename T>
1620 template <
typename T>
1629 template <
typename T>
1638 template <
typename Integer>
1639 std::vector<Integer>
1642 const std::size_t n = permutation.size();
1644 std::vector<Integer> out(n);
1645 for (std::size_t i = 0; i < n; ++i)
1646 out[i] = n - 1 - permutation[i];
1653 template <
typename Integer>
1654 std::vector<Integer>
1657 const std::size_t n = permutation.size();
1661 for (std::size_t i = 0; i < n; ++i)
1664 out[permutation[i]] = i;
1669 for (std::size_t i = 0; i < n; ++i)
1671 ExcMessage(
"The given input permutation had duplicate entries!"));
1683 namespace serialization
1690 template <
class Archive,
typename... Args>
1692 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1694 ar &std::get<
N - 1>(t);
1695 Serialize<N - 1>::serialize(ar, t, version);
1702 template <
class Archive,
typename... Args>
1704 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1712 template <
class Archive,
typename... Args>
1714 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1716 Serialize<
sizeof...(Args)>::serialize(ar, t, version);
#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
static ::ExceptionBase & ExcInternalError()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
types::global_dof_index size_type
VectorType::value_type * end(VectorType &V)
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::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::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::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
static constexpr bool value