MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
GeneralHelper.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_GENERALHELPER_HPP
2#define CDA_RAIL_GENERALHELPER_HPP
3
5#include "Definitions.hpp"
6
7#include <chrono>
8#include <cmath>
9#include <concepts>
10#include <cstddef>
11#include <filesystem>
12#include <limits>
13#include <utility>
14#include <vector>
15
16namespace cda_rail {
17
18// Comparison Helper
19
32template <std::floating_point T>
33[[nodiscard]] bool approx_equal(T const a, T const b, T const factor = 10) {
35 return std::abs(a - b) < factor * std::numeric_limits<T>::epsilon();
36}
37
38// Rounding Functions
39
52 double const tol = EPS) {
54 if (std::abs(val) < tol) {
55 val = 0;
56 }
57}
58
69[[nodiscard]] inline double round_to_given_tolerance(double const value,
70 double const tolerance) {
71 cda_rail::exceptions::throw_if_non_positive(tolerance, "tolerance");
72 auto factor = 1.0 / tolerance;
73 if (const auto rounded = std::round(factor);
74 std::abs(factor - rounded) < GRB_EPS) {
75 factor = rounded;
76 }
77 return std::round(value * factor) / factor;
78}
79
80// Combinatorial Helper
81
92[[nodiscard]] std::vector<cda_rail::index_vector>
93subsets_of_size_k_indices(size_t n, size_t k);
94
102[[nodiscard]] std::vector<std::pair<size_t, size_t>>
104
105// Debugging / Output Helper
106
115void initialize_plog(bool debug_input, bool overwrite_severity = false);
116
125[[nodiscard]] bool is_directory_and_create(const std::filesystem::path& p);
126
133[[nodiscard]] double relu(double val);
134
135template <typename Clock, typename Duration1, typename Duration2>
136[[nodiscard]] inline double get_time_difference_in_seconds(
137 std::chrono::time_point<Clock, Duration1> const& start,
138 std::chrono::time_point<Clock, Duration2> const& end) {
139 return static_cast<double>(
140 std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
141 .count()) /
142 1000.0;
143}
144
156[[nodiscard]] double get_last_time_step_before(double t, double dt,
157 bool t_inclusive);
167[[nodiscard]] double get_first_time_step_after(double t, double dt,
168 bool t_inclusive);
169
188[[nodiscard]] size_t get_last_time_index_before(double t, double dt,
189 bool t_inclusive);
205[[nodiscard]] size_t get_first_time_index_after(double t, double dt,
206 bool t_inclusive);
207
208} // namespace cda_rail
209
210#endif // CDA_RAIL_GENERALHELPER_HPP
void throw_if_non_positive(double const value, double tolerance, std::string_view const name)
Ensures a value is strictly greater than a tolerance.
Definition CustomExceptions.hpp:360
void throw_if_negative(double const value, std::string_view const name)
Validates that a value is non-negative.
Definition CustomExceptions.hpp:349
Definition CustomExceptions.hpp:15
size_t get_first_time_index_after(double t, double dt, bool t_inclusive)
return the index of the first time step after t
Definition GeneralHelper.cpp:158
bool is_directory_and_create(const std::filesystem::path &p)
Ensures that a filesystem path exists as a directory.
Definition GeneralHelper.cpp:99
std::vector< std::pair< size_t, size_t > > subsets_of_size_2_indices(size_t n)
Generates all unordered index pairs from {0, 1, ..., n - 1}.
Definition GeneralHelper.cpp:73
double get_last_time_step_before(double t, double dt, bool t_inclusive)
return last time step before t
Definition GeneralHelper.cpp:115
constexpr double EPS
Definition Definitions.hpp:26
double get_time_difference_in_seconds(std::chrono::time_point< Clock, Duration1 > const &start, std::chrono::time_point< Clock, Duration2 > const &end)
Definition GeneralHelper.hpp:136
std::vector< cda_rail::index_vector > subsets_of_size_k_indices(size_t n, size_t k)
Generates all subsets of size k of the index set {0, 1, ..., n - 1}.
Definition GeneralHelper.cpp:24
bool approx_equal(T const a, T const b, T const factor=10)
Determines if two floating-point values are approximately equal using a machine-epsilon-scaled tolera...
Definition GeneralHelper.hpp:33
size_t get_last_time_index_before(double t, double dt, bool t_inclusive)
return the index of the last time step before t
Definition GeneralHelper.cpp:143
double relu(double val)
Returns the positive part of a scalar.
Definition GeneralHelper.cpp:114
constexpr double GRB_EPS
Definition Definitions.hpp:27
void initialize_plog(bool debug_input, bool overwrite_severity=false)
Initializes the global plog console logger and adjusts its severity.
Definition GeneralHelper.cpp:89
double round_to_given_tolerance(double const value, double const tolerance)
Rounds a value to the nearest multiple of a given tolerance.
Definition GeneralHelper.hpp:69
void round_small_numbers_to_zero_inplace(double &val, double const tol=EPS)
Sets a value to zero if its absolute magnitude is less than a tolerance threshold.
Definition GeneralHelper.hpp:51
double get_first_time_step_after(double t, double dt, bool t_inclusive)
return first time step after t
Definition GeneralHelper.cpp:122