Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
NeighborGraph.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "core/Atom.hpp"
13
14#include <atomic>
15#include <mutex>
16#include <span>
17#include <vector>
18
19namespace correlation::core {
20
24struct AtomIndex {
25 size_t id;
26};
27
36
46
56public:
58 explicit NeighborGraph() = default;
59
64 explicit NeighborGraph(size_t node_count);
65
70 NeighborGraph(const NeighborGraph &other);
71
76 NeighborGraph(NeighborGraph &&other) noexcept;
77
84
90 NeighborGraph &operator=(NeighborGraph &&other) noexcept;
91
93 ~NeighborGraph() = default;
94
99 void addDirectedEdge(const Edge &edge);
100
109 const math::Vector3<real_t> &r_ij) {
111 .source = source,
112 .target = target,
113 .distance = distance,
114 .r_ij = r_ij,
115 });
116 }
117
124 void finalize() const;
125
133 void assignCSR(size_t node_count, std::vector<size_t> offsets, std::vector<Neighbor> neighbors,
134 std::vector<AtomID> targets);
135
141 [[nodiscard]] std::span<const Neighbor> getNeighbors(size_t atom_index) const;
142
148 [[nodiscard]] std::span<const AtomID> getNeighborIndices(size_t atom_index) const;
149
156 [[nodiscard]] bool areConnected(AtomIndex first_atom, AtomIndex second_atom) const;
157
162 [[nodiscard]] std::vector<bool> getDenseAdjacencyMatrix() const;
163
165 [[nodiscard]] size_t nodeCount() const noexcept;
166
168 [[nodiscard]] size_t edgeCount() const noexcept;
169
171 [[nodiscard]] std::span<const size_t> offsets() const noexcept;
172
174 [[nodiscard]] std::span<const AtomID> targets() const noexcept;
175
177 [[nodiscard]] std::span<const Neighbor> neighbors() const noexcept;
178
179private:
180 NeighborGraph(const std::scoped_lock<std::mutex> & /*lock*/, const NeighborGraph &other);
181 NeighborGraph(const std::scoped_lock<std::mutex> & /*lock*/, NeighborGraph &other) noexcept;
182
183 void ensureFinalized() const;
184
185 size_t node_count_{0};
186 mutable std::atomic<bool> is_finalized_{true};
187 mutable std::mutex finalize_mutex_;
188 mutable std::vector<Edge> unfinalized_edges_;
189 mutable std::vector<size_t> offsets_{0};
190 mutable std::vector<Neighbor> neighbors_;
191 mutable std::vector<AtomID> targets_;
192};
193
194} // namespace correlation::core
Atom data structure and AtomID type definitions.
Lightweight 3D vector and matrix classes for geometric calculations.
Compressed Sparse Row (CSR) based graph representing atomic connectivity.
Definition NeighborGraph.hpp:55
void addDirectedEdge(AtomID source, AtomID target, real_t distance, const math::Vector3< real_t > &r_ij)
Adds a directed edge between two atoms using individual parameters.
Definition NeighborGraph.hpp:108
std::span< const size_t > offsets() const noexcept
Definition NeighborGraph.cpp:264
std::vector< bool > getDenseAdjacencyMatrix() const
Generates a flattened dense adjacency matrix.
Definition NeighborGraph.cpp:233
void assignCSR(size_t node_count, std::vector< size_t > offsets, std::vector< Neighbor > neighbors, std::vector< AtomID > targets)
Directly assigns Compressed Sparse Row (CSR) storage.
Definition NeighborGraph.cpp:189
std::atomic< bool > is_finalized_
Definition NeighborGraph.hpp:186
size_t nodeCount() const noexcept
Definition NeighborGraph.cpp:257
std::vector< size_t > offsets_
Definition NeighborGraph.hpp:189
std::vector< Edge > unfinalized_edges_
Definition NeighborGraph.hpp:188
void ensureFinalized() const
Definition NeighborGraph.cpp:183
std::span< const Neighbor > getNeighbors(size_t atom_index) const
Retrieves the neighbor list for a specific atom.
Definition NeighborGraph.cpp:201
NeighborGraph()=default
Default constructor.
std::vector< AtomID > targets_
Definition NeighborGraph.hpp:191
~NeighborGraph()=default
Destructor.
size_t edgeCount() const noexcept
Definition NeighborGraph.cpp:259
void addDirectedEdge(const Edge &edge)
Adds a directed edge between two atoms.
Definition NeighborGraph.cpp:101
size_t node_count_
Definition NeighborGraph.hpp:185
NeighborGraph & operator=(const NeighborGraph &other)
Copy assignment operator.
Definition NeighborGraph.cpp:69
std::vector< Neighbor > neighbors_
Definition NeighborGraph.hpp:190
std::span< const AtomID > getNeighborIndices(size_t atom_index) const
Retrieves a lightweight index-only view of neighbors for a specific atom.
Definition NeighborGraph.cpp:211
std::span< const Neighbor > neighbors() const noexcept
Definition NeighborGraph.cpp:274
std::span< const AtomID > targets() const noexcept
Definition NeighborGraph.cpp:269
void finalize() const
Finalizes the graph into Compressed Sparse Row (CSR) storage.
Definition NeighborGraph.cpp:130
bool areConnected(AtomIndex first_atom, AtomIndex second_atom) const
Checks if two atoms are connected by an edge using binary search.
Definition NeighborGraph.cpp:221
std::mutex finalize_mutex_
Definition NeighborGraph.hpp:187
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
double real_t
Definition Precision.hpp:18
Strong type wrapper representing an atom index.
Definition NeighborGraph.hpp:24
size_t id
Internal 0-based atom index.
Definition NeighborGraph.hpp:25
Represents a directed edge connecting two atoms in the graph.
Definition NeighborGraph.hpp:40
real_t distance
Separation distance (Angstrom).
Definition NeighborGraph.hpp:43
AtomID source
Index of the source atom.
Definition NeighborGraph.hpp:41
math::Vector3< real_t > r_ij
Relative position vector from source to target.
Definition NeighborGraph.hpp:44
AtomID target
Index of the target atom.
Definition NeighborGraph.hpp:42
Represents a neighbor atom in a neighbor list.
Definition NeighborGraph.hpp:31
math::Vector3< real_t > r_ij
Vector from central atom to neighbor.
Definition NeighborGraph.hpp:34
real_t distance
Distance to the neighbor.
Definition NeighborGraph.hpp:33
AtomID index
Index of the neighbor in the atom list.
Definition NeighborGraph.hpp:32