/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
utils.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <iomanip>
5#include <iostream>
6#include <sstream>
7#include <string>
8#include <vector>
9
10// external libraries
11#include <deal.II/base/utilities.h>
12#include <spdlog/sinks/basic_file_sink.h>
13#include <spdlog/spdlog.h>
14
15// DiFfRG
17#include <DiFfRG/common/json.hh>
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
39 std::shared_ptr<spdlog::logger> build_logger(const std::string &name, const std::string &filename);
40
47 std::string strip_name(const std::string &name);
48
55 std::vector<double> string_to_double_array(const std::string &str);
56
60 template <typename T> std::string getWithPrecision(uint precision, T number)
61 {
62 std::stringstream stream;
63 stream << std::fixed << std::setprecision(precision) << number;
64 return stream.str();
65 }
66
72 bool file_exists(const std::string &name);
73
77 template <typename T> std::string to_string_with_digits(const T number, const int digits)
78 {
79 if (digits <= 0) throw std::runtime_error("to_string_with_digits: digits must be > 0");
80 const int d = int(std::ceil(std::log10(number < 0 ? -number : number)));
81 std::stringstream stream;
82 stream << std::fixed << std::setprecision(std::max(digits - d, 0)) << number;
83 return stream.str();
84 }
85
89 std::string make_folder(const std::string &path);
90
94 bool create_folder(const std::string &path_);
95
99 std::string time_format(size_t time_in_seconds);
100
101 template <typename T>
102 requires(!std::is_same_v<T, size_t>)
103 std::string time_format(T time_in_seconds)
104 {
105 return time_format(static_cast<size_t>(time_in_seconds));
106 }
107
111 std::string time_format_ms(size_t time_in_miliseconds);
112
113 bool has_suffix(const std::string &str, const std::string &suffix);
114} // 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::shared_ptr< spdlog::logger > build_logger(const std::string &name, const std::string &filename)
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:60
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:77
std::vector< double > string_to_double_array(const std::string &str)
Takes a string of comma-separated numbers and outputs it as a vector.