faunus
core.h
1 #pragma once
2 
3 #include <format>
4 #include <functional>
5 #include <iterator>
6 #include <memory>
7 #include <optional>
8 #include <ranges>
9 #include <vector>
10 #include <Eigen/Core>
11 #include <nlohmann/json.hpp>
12 
13 // forward declare logger
14 namespace spdlog {
15 class logger;
16 }
17 
18 extern template class nlohmann::basic_json<>;
19 
21 namespace Faunus {
22 
23 using Point = Eigen::Vector3d;
24 using PointVector = std::vector<Point>;
25 using json = nlohmann::json;
26 class Random;
27 
28 namespace Geometry {
30 using BoundaryFunction = std::function<void(Point&)>;
32 using DistanceFunction = std::function<Point(const Point&, const Point&)>;
33 } // namespace Geometry
34 
36 template <class T>
37 concept RequirePoints =
38  std::ranges::range<T> && std::is_same_v<std::ranges::range_value_t<T>, Point>;
39 
41 template <class T>
42 concept RequirePointIterator = std::is_convertible_v<std::iter_value_t<T>, Point>;
43 
44 using namespace std::string_literals;
45 
46 extern std::shared_ptr<spdlog::logger> faunus_logger; // global instance
47 extern std::shared_ptr<spdlog::logger> mcloop_logger; // global instance
48 
50 struct GenericError : public std::runtime_error
51 {
52  explicit GenericError(const std::exception& e);
53  explicit GenericError(const std::runtime_error& e);
54  explicit GenericError(const std::string& msg);
55  explicit GenericError(const char* msg);
56 
57  template <class... Args>
58  explicit GenericError(std::format_string<Args...> fmt, Args&&... args)
59  : std::runtime_error(std::format(fmt, std::forward<Args>(args)...))
60  {
61  }
62 };
63 
66 {
67  using GenericError::GenericError;
68  [[nodiscard]] const json& attachedJson() const;
69  ConfigurationError& attachJson(const json& j);
70 
71  private:
72  json attached_json;
73 };
74 
76 struct IOError : public GenericError
77 {
78  using GenericError::GenericError;
79 };
80 
88 void displayError(spdlog::logger& logger, const std::exception& e, int level = 0);
89 
90 } // namespace Faunus
nlohmann::json json
JSON object.
Definition: json_support.h:10
Eigen::Vector3d Point
3D vector used for positions, velocities, forces etc.
Definition: coordinates.h:7
std::function< Point(const Point &, const Point &)> DistanceFunction
Function to calculate the (minimum) distance between two points.
Definition: core.h:32
Random number generator.
Definition: random.h:34
std::vector< Point > PointVector
Vector of 3D vectors.
Definition: core.h:24
Exception to be thrown when parsing json configuration.
Definition: core.h:65
Exception to be thrown on IO errors.
Definition: core.h:76
Common ancestor of Faunus specific runtime errors.
Definition: core.h:50
Definition: core.h:14
std::function< void(Point &)> BoundaryFunction
Function to apply PBC to a position.
Definition: core.h:30
Cell list class templates.
Definition: actions.cpp:11
void displayError(spdlog::logger &logger, const std::exception &e, int level=0)
Nicely displays nested exceptions using a logger.
concept RequirePointIterator
Concept for an iterator to a Point
Definition: core.h:42
concept RequirePoints
Concept for a range of points.
Definition: core.h:37