11#ifndef INCLUDE_UTILITIES_HPP_
12#define INCLUDE_UTILITIES_HPP_
29#include "pcg_random.hpp"
32#include <fmt/ostream.h>
35#include <spdlog/sinks/basic_file_sink.h>
36#include <spdlog/sinks/stdout_color_sinks.h>
37#include <spdlog/spdlog.h>
57 case topology_type::SPHERICAL:
return t_os <<
"spherical";
58 case topology_type::TOROIDAL:
return t_os <<
"toroidal";
59 default:
return t_os <<
"none";
80 date::zoned_time
const time(date::current_zone(),
81 std::chrono::system_clock::now());
82 return format(
"%Y-%m-%d.%X%Z", time);
96 double t_initial_radius,
97 double t_foliation_spacing)
noexcept -> std::filesystem::path
100 if (t_topology == topology_type::SPHERICAL) { filename +=
"S"; }
101 else { filename +=
"T"; }
103 filename += std::to_string(t_dimension);
107 filename += std::to_string(t_number_of_timeslices);
111 filename += std::to_string(t_number_of_simplices);
115 filename += std::to_string(t_initial_radius);
119 filename += std::to_string(t_foliation_spacing);
130 template <
typename ManifoldType>
131 [[nodiscard]]
auto make_filename(ManifoldType
const& manifold)
133 return make_filename(ManifoldType::topology, ManifoldType::dimension,
134 manifold.N3(), manifold.max_time(),
135 manifold.initial_radius(),
136 manifold.foliation_spacing());
143 template <
typename TriangulationType>
147 "Triangulation has {} vertices and {} edges and {} faces and {} "
149 t_triangulation.number_of_vertices(),
150 t_triangulation.number_of_finite_edges(),
151 t_triangulation.number_of_finite_facets(),
152 t_triangulation.number_of_finite_cells());
162 template <
typename TriangulationType>
164 TriangulationType triangulation)
166 static std::mutex mutex;
167 fmt::print(
"Writing to file {}\n", filename.string());
168 std::scoped_lock
const lock(mutex);
169 std::ofstream file(filename, std::ios::out);
172 throw std::filesystem::filesystem_error(
173 "Could not open file for writing", filename,
174 std::make_error_code(std::errc::bad_file_descriptor));
176 file << triangulation;
185 template <
typename ManifoldType>
188 std::filesystem::path filename;
190 write_file(filename, t_universe.get_delaunay());
197 template <
typename TriangulationType>
198 auto read_file(std::filesystem::path
const& filename) -> TriangulationType
200 static std::mutex mutex;
201 fmt::print(
"Reading from file {}\n", filename.string());
202 std::scoped_lock
const lock(mutex);
203 std::ifstream file(filename, std::ios::in);
206 throw std::filesystem::filesystem_error(
207 "Could not open file for reading", filename,
208 std::make_error_code(std::errc::bad_file_descriptor));
210 TriangulationType triangulation;
211 file >> triangulation;
212 return triangulation;
218 pcg_extras::seed_seq_from<std::random_device> seed_source;
221 pcg64 rng(seed_source);
224 std::uniform_int_distribution uniform_dist(1, 6);
242 template <
typename NumberType,
class Distribution>
244 NumberType t_max_value)
noexcept
246 pcg_extras::seed_seq_from<std::random_device> seed_source;
248 pcg64 generator(seed_source);
249 Distribution distribution(t_min_value, t_max_value);
250 return distribution(generator);
257 pcg_extras::seed_seq_from<std::random_device> seed_source;
258 pcg64 generator(seed_source);
264 template <
typename IntegerType>
266 IntegerType t_min_value, IntegerType t_max_value)
noexcept
268 using int_dist = std::uniform_int_distribution<IntegerType>;
269 return generate_random<IntegerType, int_dist>(t_min_value, t_max_value);
273 template <
typename IntegerType>
275 IntegerType&& t_max_timeslice)
noexcept ->
decltype(
auto)
278 std::forward<IntegerType>(t_max_timeslice));
283 template <
typename FloatingPo
intType>
285 FloatingPointType t_min_value, FloatingPointType t_max_value)
noexcept
287 using real_dist = std::uniform_real_distribution<FloatingPointType>;
288 return generate_random<FloatingPointType, real_dist>(t_min_value,
295 auto constexpr min = 0.0L;
296 auto constexpr max = 1.0L;
318 spdlog::debug(
"{} simplices on {} timeslices desired.\n",
319 t_number_of_simplices, t_number_of_timeslices);
322 auto const simplices_per_timeslice =
323 t_number_of_simplices / t_number_of_timeslices;
324 if (t_dimension == 3)
327 if (t_number_of_simplices == t_number_of_timeslices)
329 return 2 * simplices_per_timeslice;
331 if (t_number_of_simplices < 1000)
335 static_cast<long double>(simplices_per_timeslice));
337 if (t_number_of_simplices < 10000)
341 static_cast<long double>(simplices_per_timeslice));
343 if (t_number_of_simplices < 100000)
347 static_cast<long double>(simplices_per_timeslice));
351 0.1L *
static_cast<long double>(simplices_per_timeslice));
353 throw std::invalid_argument(
"Currently, dimensions cannot be >3.");
367 return t_value.to_double();
397 auto const console_sink =
398 std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
399 console_sink->set_level(spdlog::level::info);
401 auto const debug_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(
402 "logs/debug-log.txt",
true);
403 debug_sink->set_level(spdlog::level::debug);
405 auto const trace_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(
406 "logs/trace-log.txt",
true);
407 trace_sink->set_level(spdlog::level::trace);
409 spdlog::sinks_init_list sink_list = {console_sink, debug_sink, trace_sink};
411 auto const logger = std::make_shared<spdlog::logger>(
412 "multi_sink", sink_list.begin(), sink_list.end());
414 logger->set_level(spdlog::level::trace);
416 logger->info(
"Multi-sink logger initialized.\n");
417 logger->debug(
"Debug logger initialized.\n");
418 logger->trace(
"Trace logger initialized.\n");
420 "You must build in Debug mode for anything to be recorded in this "
423 register_logger(logger);
424 set_default_logger(logger);
426 catch (spdlog::spdlog_ex
const& ex)
429 spdlog::error(
"Logger initialization failed: {}\n", ex.what());
430 spdlog::warn(
"Default logger set.\n");
438 template <
typename Po
int>
441 std::stringstream stream;
451 std::stringstream stream;
452 stream << t_topology;
Global integer and precision settings.
std::int_fast32_t Int_precision
auto operator<<(std::ostream &t_os, topology_type const &t_topology) -> std::ostream &
Convert topology_type to string output.
auto topology_to_str(topology_type const &t_topology) -> std::string
Convert a topology to a string using it's << operator.
auto generate_random_timeslice(IntegerType &&t_max_timeslice) noexcept -> decltype(auto)
Generate a random timeslice.
auto die_roll() noexcept
Roll a die with PCG.
void print_delaunay(TriangulationType const &t_triangulation)
Print triangulation statistics.
auto read_file(std::filesystem::path const &filename) -> TriangulationType
Read triangulation from file.
auto make_filename(topology_type const &t_topology, Int_precision t_dimension, Int_precision t_number_of_simplices, Int_precision t_number_of_timeslices, double t_initial_radius, double t_foliation_spacing) noexcept -> std::filesystem::path
Generate useful filenames.
auto point_to_str(Point const &t_point) -> std::string
Covert a CGAL point to a string.
auto constexpr generate_probability() noexcept
Generate a probability.
auto Gmpzf_to_double(Gmpzf const &t_value) -> double
Convert Gmpzf into a double.
auto current_date_time()
Return current date and time.
auto make_random_generator() noexcept
Make a high-quality random number generator usable by std::shuffle.
void create_logger()
Create console and file loggers.
void write_file(std::filesystem::path const &filename, TriangulationType triangulation)
Write triangulation to file.
auto constexpr generate_random_int(IntegerType t_min_value, IntegerType t_max_value) noexcept
Generate random integers by calling generate_random, preserves template argument deduction.
auto constexpr generate_random_real(FloatingPointType t_min_value, FloatingPointType t_max_value) noexcept
Generate random real numbers by calling generate_random, preserves template argument deduction.
topology_type
clang-15 does not support std::format
auto expected_points_per_timeslice(Int_precision const t_dimension, Int_precision t_number_of_simplices, Int_precision t_number_of_timeslices)
Calculate expected # of points per simplex.
auto generate_random(NumberType t_min_value, NumberType t_max_value) noexcept
Generate random numbers.