/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/common/utils.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/common/utils.hh Source File
DiFfRG
Discretization Framework for functional Renormalization Group flows
utils.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <concepts>
5#include <iomanip>
6#include <iostream>
7#include <sstream>
8#include <string>
9#include <type_traits>
10#include <vector>
11
12// external libraries
13#include <deal.II/base/utilities.h>
14
15// DiFfRG
19#include <DiFfRG/common/math.hh>
21
22namespace DiFfRG
23{
24 using uint = unsigned int;
25
26 using dealii::Utilities::int_to_string;
27
31 template <auto Start, auto End, auto Inc, class F> constexpr void constexpr_for(F &&f)
32 {
33 if constexpr (Start < End) {
34 f(std::integral_constant<decltype(Start), Start>());
36 }
37 }
38
45 std::string strip_name(const std::string &name);
46
50 template <typename T> std::string getWithPrecision(uint precision, T number)
51 {
52 std::stringstream stream;
53 stream << std::fixed << std::setprecision(precision) << number;
54 return stream.str();
55 }
56
62 bool file_exists(const std::string &name);
63
67 template <typename T> std::string to_string_with_digits(const T number, const int digits)
68 {
69 if (digits <= 0) throw std::runtime_error("to_string_with_digits: digits must be > 0");
70 const int d = int(std::ceil(std::log10(number < 0 ? -number : number)));
71 std::stringstream stream;
72 stream << std::fixed << std::setprecision(std::max(digits - d, 0)) << number;
73 return stream.str();
74 }
75
79 std::string make_folder(const std::string &path);
80
84 bool create_folder(const std::string &path_);
85
89 std::string time_format(size_t time_in_seconds);
90
91 template <typename T>
92 requires(!std::is_same_v<T, size_t>)
93 std::string time_format(T time_in_seconds)
94 {
95 return time_format(static_cast<size_t>(time_in_seconds));
96 }
97
101 std::string time_format_ms(size_t time_in_miliseconds);
102
103 bool has_suffix(const std::string &str, const std::string &suffix);
104} // namespace DiFfRG
Definition complex_math.hh:10
std::string make_folder(const std::string &path)
Add a trailing '/' to a string, in order for it to be in standard form of a folder path.
bool create_folder(const std::string &path_)
Creates the directory path, even if its parent directories should not exist.
std::string time_format_ms(size_t time_in_miliseconds)
Nice output from seconds to h/min/s style string.
std::string getWithPrecision(uint precision, T number)
Return number with fixed precision after the decimal point.
Definition utils.hh:50
bool has_suffix(const std::string &str, const std::string &suffix)
bool file_exists(const std::string &name)
Checks if a file exists.
constexpr void constexpr_for(F &&f)
A compile-time for loop, which calls the lambda f of signature void(integer) for each index.
Definition utils.hh:31
std::string time_format(size_t time_in_seconds)
Nice output from seconds to h/min/s style string.
unsigned int uint
Definition utils.hh:24
std::string strip_name(const std::string &name)
Strips all special characters from a string, e.g. for use in filenames.
std::string to_string_with_digits(const T number, const int digits)
Return number with fixed significant digits.
Definition utils.hh:67