Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
Atom.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
12#include "math/Precision.hpp"
13
14#include <algorithm>
15#include <array>
16#include <atomic>
17#include <cstdint>
18#include <memory>
19#include <mutex>
20#include <stdexcept>
21#include <string>
22#include <string_view>
23#include <unordered_map>
24#include <vector>
25
26namespace correlation::core {
27
31using AtomID = std::uint32_t;
32
36struct ElementID {
37 std::int32_t value{
38 -1,
39 };
40
46 constexpr bool operator==(const ElementID &other) const = default;
47};
48
52struct Element {
53 std::string symbol;
54 ElementID id{-1};
55
61 constexpr bool operator==(const Element &other) const { return symbol == other.symbol; }
62};
63
68public:
69 static constexpr std::size_t kMaxElements = 65536;
70
75 [[nodiscard]] static const Element *defaultElement() noexcept {
76 static const Element DEFAULT_ELEM{.symbol = "", .id = ElementID{-1}};
77 return &DEFAULT_ELEM;
78 }
79
85 [[nodiscard]] static const Element &get(std::uint16_t index) noexcept {
86 const Element *ptr = table()[index].load(std::memory_order_acquire);
87 return ptr != nullptr ? *ptr : *defaultElement();
88 }
89
95 [[nodiscard]] static std::uint16_t internIndex(const Element &elem) {
96 if (elem.symbol.empty() && elem.id.value == -1) {
97 return 0;
98 }
99 return internIndex(elem.symbol, elem.id);
100 }
101
108 [[nodiscard]] static std::uint16_t internIndex(std::string_view symbol, ElementID elem_id) {
109 if (symbol.empty() && elem_id.value == -1) {
110 return 0;
111 }
112 struct Key {
113 std::string symbol;
114 std::int32_t id_val;
115 bool operator==(const Key &other) const noexcept {
116 return id_val == other.id_val && symbol == other.symbol;
117 }
118 };
119 struct KeyHash {
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);
123 }
124 };
125
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;
129
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()) {
133 return iter->second;
134 }
135
136 const std::size_t next_slot = storage.size() + 1;
137 if (next_slot >= kMaxElements) {
138 throw std::overflow_error("ElementPool capacity exceeded (max 65535 distinct elements).");
139 }
140
141 auto new_element =
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);
148 return idx;
149 }
150
156 [[nodiscard]] static const Element *intern(const Element &elem) {
157 return &get(internIndex(elem));
158 }
159
166 [[nodiscard]] static const Element *intern(std::string_view symbol, ElementID elem_id) {
167 return &get(internIndex(symbol, elem_id));
168 }
169
170private:
171 static std::array<std::atomic<const Element *>, kMaxElements> &table() noexcept {
172 static std::array<std::atomic<const Element *>, kMaxElements> entries{};
173 return entries;
174 }
175};
176
183class Atom {
184public:
187 explicit Atom() noexcept = default;
188
195 explicit Atom(const Element &element, const math::Vector3<real_t> &pos, AtomID atom_id)
196 : position_(pos), id_(atom_id), element_pool_idx_(ElementPool::internIndex(element)),
197 element_id_(static_cast<std::int16_t>(element.id.value)) {}
198
205 explicit Atom(const Element *element_ptr, const math::Vector3<real_t> &pos, AtomID atom_id)
206 : position_(pos), id_(atom_id),
207 element_pool_idx_(element_ptr != nullptr ? ElementPool::internIndex(*element_ptr) : 0),
208 element_id_(element_ptr != nullptr ? static_cast<std::int16_t>(element_ptr->id.value)
209 : static_cast<std::int16_t>(-1)) {}
210
212
215
220 [[nodiscard]] AtomID id() const noexcept { return id_; }
221
226 void setID(std::uint32_t num) noexcept { id_ = num; }
227
232 [[nodiscard]] const math::Vector3<real_t> &position() const noexcept { return position_; }
233
238 void setPosition(const math::Vector3<real_t> &pos) noexcept { position_ = pos; }
239
244 [[nodiscard]] const Element &element() const noexcept {
246 }
247
252 void setElement(const Element &ele) {
254 element_id_ = static_cast<std::int16_t>(ele.id.value);
255 }
256
261 [[nodiscard]] std::int32_t elementId() const noexcept {
262 return static_cast<std::int32_t>(element_id_);
263 }
264
266
267private:
270 std::uint16_t element_pool_idx_{0};
271 std::int16_t element_id_{-1};
272};
273
280[[nodiscard]] inline real_t distance(const Atom &atom_a, const Atom &atom_b) noexcept {
281 return math::norm(atom_a.position() - atom_b.position());
282}
283
291[[nodiscard]] inline real_t angle(const Atom &center, const Atom &atom_a,
292 const Atom &atom_b) noexcept {
293 const math::Vector3<real_t> vec_a = atom_a.position() - center.position();
294 const math::Vector3<real_t> vec_b = atom_b.position() - center.position();
295
296 const real_t norm_sq_a = math::dot(vec_a, vec_a);
297 const real_t norm_sq_b = math::dot(vec_b, vec_b);
298
299 // Guard against near-zero norms to prevent division by zero or NaN underflows
300 if (norm_sq_a < 1e-12 || norm_sq_b < 1e-12) {
301 return 0.0;
302 }
303
304 using std::acos;
305 using std::sqrt;
306 real_t cos_theta = math::dot(vec_a, vec_b) / sqrt(norm_sq_a * norm_sq_b);
307
308 // Clamp for numerical stability
309 cos_theta = std::clamp(cos_theta, static_cast<real_t>(-1.0), static_cast<real_t>(1.0));
310
311 return acos(cos_theta);
312}
313
314} // namespace correlation::core
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
Atom() noexcept=default
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 &center, 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