23#include <unordered_map>
85 [[nodiscard]]
static const Element &
get(std::uint16_t index)
noexcept {
86 const Element *ptr =
table()[index].load(std::memory_order_acquire);
109 if (symbol.empty() && elem_id.
value == -1) {
115 bool operator==(
const Key &other)
const noexcept {
116 return id_val == other.id_val && symbol == other.symbol;
120 size_t operator()(
const Key &key_obj)
const noexcept {
121 return std::hash<std::string_view>{}(key_obj.symbol) ^
122 (std::hash<std::int32_t>{}(key_obj.id_val) << 1);
126 static std::mutex pool_mutex;
127 static std::unordered_map<Key, std::uint16_t, KeyHash> index_map;
128 static std::vector<std::unique_ptr<Element>> storage;
130 std::scoped_lock lock(pool_mutex);
131 Key
const key{std::string(symbol), elem_id.
value};
132 if (
const auto iter = index_map.find(key); iter != index_map.end()) {
136 const std::size_t next_slot = storage.size() + 1;
138 throw std::overflow_error(
"ElementPool capacity exceeded (max 65535 distinct elements).");
142 std::unique_ptr<Element>(
new Element{.symbol = std::string(symbol), .id = elem_id});
143 const Element *ptr = new_element.get();
144 storage.push_back(std::move(new_element));
145 const auto idx =
static_cast<std::uint16_t
>(next_slot);
146 table()[idx].store(ptr, std::memory_order_release);
147 index_map.emplace(key, idx);
172 static std::array<std::atomic<const Element *>,
kMaxElements> entries{};
187 explicit Atom() noexcept = default;
208 element_id_(element_ptr != nullptr ? static_cast<std::int16_t>(element_ptr->
id.value)
209 : static_cast<std::int16_t>(-1)) {}
226 void setID(std::uint32_t num)
noexcept {
id_ = num; }
281 return math::norm(atom_a.position() - atom_b.position());
292 const Atom &atom_b)
noexcept {
300 if (norm_sq_a < 1e-12 || norm_sq_b < 1e-12) {
306 real_t cos_theta =
math::dot(vec_a, vec_b) / sqrt(norm_sq_a * norm_sq_b);
309 cos_theta = std::clamp(cos_theta,
static_cast<real_t>(-1.0),
static_cast<real_t>(1.0));
311 return acos(cos_theta);
Lightweight 3D vector and matrix classes for geometric calculations.
Floating point precision type definitions for Correlation.
Represents an atom in the simulation cell.
Definition Atom.hpp:183
std::int32_t elementId() const noexcept
Gets the integer ID of the element type.
Definition Atom.hpp:261
const Element & element() const noexcept
Gets the element type of the atom.
Definition Atom.hpp:244
void setPosition(const math::Vector3< real_t > &pos) noexcept
Sets the position of the atom.
Definition Atom.hpp:238
math::Vector3< real_t > position_
Cartesian coordinates in Angstroms.
Definition Atom.hpp:268
std::uint16_t element_pool_idx_
Compact index into ElementPool.
Definition Atom.hpp:270
void setID(std::uint32_t num) noexcept
Sets the unique ID of the atom.
Definition Atom.hpp:226
const math::Vector3< real_t > & position() const noexcept
Gets the position of the atom.
Definition Atom.hpp:232
AtomID id_
Unique identification number.
Definition Atom.hpp:269
AtomID id() const noexcept
Gets the unique ID of the atom.
Definition Atom.hpp:220
std::int16_t element_id_
Cached integer element ID (-1 if unassigned).
Definition Atom.hpp:271
void setElement(const Element &ele)
Sets the element type of the atom.
Definition Atom.hpp:252
Atom(const Element *element_ptr, const math::Vector3< real_t > &pos, AtomID atom_id)
Pointer-based flyweight constructor.
Definition Atom.hpp:205
Thread-safe flyweight pool for chemical Element instances.
Definition Atom.hpp:67
static std::uint16_t internIndex(std::string_view symbol, ElementID elem_id)
Interns an element by symbol and ID and returns its compact 16-bit pool index.
Definition Atom.hpp:108
static const Element & get(std::uint16_t index) noexcept
Lock-free O(1) lookup of an interned Element by its compact 16-bit pool index.
Definition Atom.hpp:85
static const Element * intern(const Element &elem)
Interns an Element into the pool or returns an existing instance.
Definition Atom.hpp:156
static std::uint16_t internIndex(const Element &elem)
Interns an Element and returns its compact 16-bit pool index.
Definition Atom.hpp:95
static const Element * intern(std::string_view symbol, ElementID elem_id)
Interns an element by symbol and ID into the pool.
Definition Atom.hpp:166
static const Element * defaultElement() noexcept
Returns a shared sentinel empty element instance.
Definition Atom.hpp:75
static constexpr std::size_t kMaxElements
Definition Atom.hpp:69
static std::array< std::atomic< const Element * >, kMaxElements > & table() noexcept
Definition Atom.hpp:171
A lightweight, constexpr, stack-based 3D vector.
Definition LinearAlgebra.hpp:32
Definition ITrajectoryCalculator.hpp:13
real_t distance(const Atom &atom_a, const Atom &atom_b) noexcept
Calculates the Euclidean distance between two atoms.
Definition Atom.hpp:280
std::uint32_t AtomID
Unsigned integer type used for unique atom identification.
Definition Atom.hpp:31
real_t angle(const Atom ¢er, const Atom &atom_a, const Atom &atom_b) noexcept
Calculates the angle (in radians) formed by three atoms.
Definition Atom.hpp:291
Defines universal mathematical and physical constants.
constexpr T dot(const Vector3< T > &vec_a, const Vector3< T > &vec_b) noexcept
Computes the dot product of two vectors.
Definition LinearAlgebra.hpp:466
constexpr T norm(const Vector3< T > &vec_a) noexcept
Computes the vector norm (length).
Definition LinearAlgebra.hpp:520
double real_t
Definition Precision.hpp:18
Represents a unique integer ID for an element type.
Definition Atom.hpp:36
std::int32_t value
Unique integer value representing the element type (-1 for unassigned).
Definition Atom.hpp:37
constexpr bool operator==(const ElementID &other) const =default
Equality operator for ElementID.
Represents a chemical element with its symbol and unique ID.
Definition Atom.hpp:52
constexpr bool operator==(const Element &other) const
Equality operator for Element.
Definition Atom.hpp:61
ElementID id
Assigned unique integer ID.
Definition Atom.hpp:54
std::string symbol
Chemical symbol (e.g., "Si").
Definition Atom.hpp:53